472,953 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,953 software developers and data experts.

views dependent on other views

Hello There,

I'm trying to create a view that has calculations dependent on
calculations, where the problem resides is that each time I make a
calculation I must create an intermediate view so I can reference a
previous calculation.

for example lets say I have my_table that has columns a & b. now I want
a view that has a & b, c = a + b, and d = c + 1.

this is grossly simplified, the calculations I actually use are fairly
complex and copying / pasting them is out of the question.

so what I have is my_view_a which makes column c, and my my_view_final
which makes column d (however, in my real application I have 5 of these
views, a/b/c/d/e/)

is there anyway I can consolidate all these views into one? I was
thinking of using a stored procedure with temp tables or something
along those lines.

I just which I can use the aliases that I create for c in d in one
step.

any insight would be greatly appreciated.

Mar 14 '06 #1
5 1856
On 14 Mar 2006 12:29:25 -0800, lu*****@gmail.com wrote:
Hello There,

I'm trying to create a view that has calculations dependent on
calculations, where the problem resides is that each time I make a
calculation I must create an intermediate view so I can reference a
previous calculation.

for example lets say I have my_table that has columns a & b. now I want
a view that has a & b, c = a + b, and d = c + 1.

this is grossly simplified, the calculations I actually use are fairly
complex and copying / pasting them is out of the question.

so what I have is my_view_a which makes column c, and my my_view_final
which makes column d (however, in my real application I have 5 of these
views, a/b/c/d/e/)

is there anyway I can consolidate all these views into one? I was
thinking of using a stored procedure with temp tables or something
along those lines.

I just which I can use the aliases that I create for c in d in one
step.

any insight would be greatly appreciated.


Hi lukster,

You can use derived tables instead of views:

SELECT a, b, c, c + 1 AS d
FROM (SELECT a, b, a + b AS c
FROM SomeTable
WHERE .....
) AS Der
WHERE .....

You can nest this if yoou need to.

Another technique is to repeat the calculation:

SELECT a, b, a + b AS c, (a + b) + 1 AS d
FROM SomeTable
WHERE .....

--
Hugo Kornelis, SQL Server MVP
Mar 14 '06 #2
(lu*****@gmail.com) writes:
I'm trying to create a view that has calculations dependent on
calculations, where the problem resides is that each time I make a
calculation I must create an intermediate view so I can reference a
previous calculation.

for example lets say I have my_table that has columns a & b. now I want
a view that has a & b, c = a + b, and d = c + 1.

this is grossly simplified, the calculations I actually use are fairly
complex and copying / pasting them is out of the question.

so what I have is my_view_a which makes column c, and my my_view_final
which makes column d (however, in my real application I have 5 of these
views, a/b/c/d/e/)

is there anyway I can consolidate all these views into one? I was
thinking of using a stored procedure with temp tables or something
along those lines.

I just which I can use the aliases that I create for c in d in one
step.


I will have to take a long shot and guess what you are looking for.
I believe that the answer to your question is derived tables. A
derived table is sort of a temp table within the query, but only
logically. The actual computation order can often be different, as
long as the result is the same. Here is a quick example from the
Northind database that presents the number of orders per season.
The query features two derived tables. The innermost extracts day
and month from OrderDate, and the outer table translates the date
to a season.

SELECT Season, COUNT(*)
FROM (SELECT Season = CASE WHEN daymonth BETWEEN '0101' AND '0315' OR
daymonth BETWEEN '1201' AND '1231'
THEN 'Winter'
WHEN daymonth BETWEEN '0316' AND '0531'
THEN 'Spring'
WHEN daymonth BETWEEN '0601' AND '0831'
THEN 'Summer'
WHEN daymonth BETWEEN '0901' AND '1130'
THEN 'Autumn'
END
FROM (SELECT daymonth =
substring(convert(char(8), OrderDate, 112), 5, 4)
FROM Orders) AS O) AS O2
GROUP BY Season

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 14 '06 #3
>> is there anyway I can consolidate all these views into one? <<

Sure, but why? You can nest VIEWs without any problems. Those other
views were probably constructed for a good reason. "Trust in the
Optimizer, Luke!"

However, look up the WITH CHECK OPTION on VIEWs; could be useful.

Mar 15 '06 #4
hmmm. use parenthesis. think about it logically step by step.

if you can do it with views, you can do it in a single select, if you
use good formatting and parentesis to keep your brain straight around
it.

luck
doug

Mar 16 '06 #5
Derived is exactly what I need,

thanks everyone for the prompt responses.

just in reponse to some of the comments:

Repeating wouldn't work because for what I'm doin' column 'c' is a
lengthy case statement which 4-5 other colums depend on, copy/pasting
that over and over woudl make the query unreadable.

why eleminate the views? I'd like to minimize the amount of views
because it's clutters my database, I'm well over 200 views now, most of
which are just intermediate steps, also. when working on the final view
I'd like to see all the steps at once, writing documentation for 5
views and having flip between them is annoying (especially with now
slow access is)

once again, thank you very much.

Mar 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike N. | last post by:
Hello: I am new to T-SQL programing, and relativly new to SQL statements in general, although I have a good understanding of database theory. I'm a little confused as to the fundamental...
3
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm...
3
by: teddysnips | last post by:
This from a SQL Server manual: "Complex queries, however, such as those in decision support systems, can reference large numbers of rows in base tables and aggregate large amounts of information...
3
by: dbtoo_dbtoo | last post by:
One of the databases has 50 views and when I do a db2look I only get schema for 40 of them. If I select from the sysviews, I can see all 50 (the text column contains schema for all 10 (missing)...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
3
by: KemperR | last post by:
Hello Experts outhere, may be someone can tell me whats going wrong with my ADOX trial. I have an Access 2002 database with some tables and queries (views) The code listed below works well up...
2
by: dbuchanan52 | last post by:
Hello, I am building an application for Windows Forms using. I am new to SQL Server 'Views'. Are the following correct understanding of their use? 1.) I believe a view can be referenced in a...
15
by: rod.weir | last post by:
Fellow database developers, I would like to draw on your experience with views. I have a database that includes many views. Sometimes, views contains other views, and those views in turn may...
2
by: puzzlecracker | last post by:
See it a lot but haven't learn the difference between this two in the context of template. Would someone explain it please? Thanks
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.