Use a calculated value in multiple places in a query
-
I am building a query which returns a few calculated values. Some of these are as simple as ColA - ColB, while others are far more complex - but are often based on the result of a previous result. My question is, how can I reuse these fields that I have calculated? A lot of the time that I do these calculations, I also need to exclude some values in the WHERE clause, based on these answers. Example: SELECT ColA - ColB [Result] FROM Table WHERE (ColA - ColB) > 0 How could I calculate ColA - ColB just once - and use the result of that in the where clause or another column? Thanks in advance
-
I am building a query which returns a few calculated values. Some of these are as simple as ColA - ColB, while others are far more complex - but are often based on the result of a previous result. My question is, how can I reuse these fields that I have calculated? A lot of the time that I do these calculations, I also need to exclude some values in the WHERE clause, based on these answers. Example: SELECT ColA - ColB [Result] FROM Table WHERE (ColA - ColB) > 0 How could I calculate ColA - ColB just once - and use the result of that in the where clause or another column? Thanks in advance
-
One way is to use inline views, like:
SELECT alias1.Result
FROM (SELECT (ColA - ColB) AS Result
FROM Table) alias1
WHERE alias1.result > 0The need to optimize rises from a bad design.My articles[^]
-
One way is to use inline views, like:
SELECT alias1.Result
FROM (SELECT (ColA - ColB) AS Result
FROM Table) alias1
WHERE alias1.result > 0The need to optimize rises from a bad design.My articles[^]
In SQL Server 2005+ you can use CTEs (Common Table Expressions) to do the same kind of thing:
WITH Results AS
(
SELECT ColA - ColB [Result]
FROM TABLE
)
SELECT *
FROM Results
WHERE Result > 0Or with a temp table like so:
SELECT ColA - ColB [Result]
INTO #temp
FROM TABLESELECT *
FROM #temp
WHERE Result > 0DROP TABLE #temp
Or with a table variable like so:
DECLARE @temp TABLE (Result int)
INSERT @temp
SELECT ColA - ColB [Result]
FROM TABLESELECT *
FROM @temp
WHERE Result > 0I prefer to use the CTE because I think it's less code and easily readable.
Keep It Simple Stupid! (KISS)
-
In SQL Server 2005+ you can use CTEs (Common Table Expressions) to do the same kind of thing:
WITH Results AS
(
SELECT ColA - ColB [Result]
FROM TABLE
)
SELECT *
FROM Results
WHERE Result > 0Or with a temp table like so:
SELECT ColA - ColB [Result]
INTO #temp
FROM TABLESELECT *
FROM #temp
WHERE Result > 0DROP TABLE #temp
Or with a table variable like so:
DECLARE @temp TABLE (Result int)
INSERT @temp
SELECT ColA - ColB [Result]
FROM TABLESELECT *
FROM @temp
WHERE Result > 0I prefer to use the CTE because I think it's less code and easily readable.
Keep It Simple Stupid! (KISS)
That's true and leads to the same result. Writing inlines is just a (bad) habit that always comes first in mind. Guess it's because I've written them over fifteen years so I believe it's like teaching an old dog... :)
The need to optimize rises from a bad design.My articles[^]
-
Thanks for the reply. I was just kind of hoping there was a way to store the value in a temp variable on a row by row basis, but using inline views does the trick as well - so thanks.
toticow wrote:
I was just kind of hoping there was a way to store the value in a temp variable on a row by row basis
That won't be possible (and wouldn't be efficient) since sql is set based language. What you were hoping for would be record based and this would be close to cursor handling (and of course could be implemented in an procedure using cursors). But you don't want to use cursors since (as Mycroft in this forum said) they are evil :) Also have a look at other set based variations Ben Fair posted.
toticow wrote:
so thanks
You're welcome.
The need to optimize rises from a bad design.My articles[^]
-
That's true and leads to the same result. Writing inlines is just a (bad) habit that always comes first in mind. Guess it's because I've written them over fifteen years so I believe it's like teaching an old dog... :)
The need to optimize rises from a bad design.My articles[^]
-
That's true and leads to the same result. Writing inlines is just a (bad) habit that always comes first in mind. Guess it's because I've written them over fifteen years so I believe it's like teaching an old dog... :)
The need to optimize rises from a bad design.My articles[^]
Mika Wendelius wrote:
comes first in mind
Probably more portable too?
-
Mika Wendelius wrote:
comes first in mind
Probably more portable too?