Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Use calculated value in the same select
Message
General information
Forum:
ASP.NET
Category:
Databases
Miscellaneous
Thread ID:
00901314
Message ID:
00901342
Views:
24
This message has been marked as the solution to the initial question of the thread.
SQL Server won't give you the behavior that you're looking for. I can think of two ways to get the behavior that your looking for.

1) Use views

CREATE VIEW qtrs AS
SELECT SUM(jan+feb+mar) AS qtr1, SUM(apr+may+jun) AS qtr2 FROM myTable

SELECT qtr1, qtr2, qtr1+qtr2 AS firstHalf FROM qtrs

2) Use derived tables

SELECT qtr1, qtr2, qtr1+qtr2 AS firstHalf FROM (
SELECT SUM(jan+feb+mar) AS qtr1, SUM(apr+may+jun) AS qtr2 FROM myTable )

-Mike


>Bonnie,
>
>Thank you for the reply. I am using SQL Server and I had thought about your suggestion, but the calculations are more complicated than the exmaple I gave. So it would be a lot cleaner if I could use the calculated results.
>
>Kind Regards,
>
>Mat
>
>>Mat,
>>
>>I'd say it depends on what database you're using. SQL Server does not support this type of syntax, but I don't know about other databases (like maybe Oracle). BTW, for the particular example you gave, you could always do something like this instead:
>>
>>select sum(jan+feb+mar) as qtr1,
>>       sum(apr+may+jun) as qtr2,
>>       sum(jan+feb+mar+apr+may+jun) as first6months
>>  from myTable
>>
>>
>>
>>~~Bonnie
>>
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform