Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / T-SQL
Tip/Trick

How to Pass Calculated Columns in the WHERE & GROUP BY clause in T-SQL..?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
13 Jun 2013CPOL 62.8K   7   3
Passing calculated columns in the Where and Group by clause in T-SQL

Introduction

Suppose we have a calculated Column that is a result of some complex calculation and we want to refer it in a where clause (or a group by clause). This is not straight forward since we can’t use the computed Column directly in the where clause like this:

SQL
Select 	Column1,
  	Column2, 
  	Calculated_Column = 	
    	Case 
      		When Expression1 = True Then Column1 * (Column4 - Column 5)
      		When Expression2 = True Then Column2 * (Column5 - Column 6)
      		When Expression3 = True Then Column3 * (Column6 - Column 7)
      		Else 
		Column4 - Column 5
    	End
From
	Table			
Where
	Calculated_Column < 100

The reason is that the WHERE part of the query is evaluated before the Select part so, we need to duplicate the whole calculation in the WHERE clause like this:

SQL
Where
     (
      Case 
          When Expression1 = True Then Column1 * (Column4 - Column 5)
          When Expression2 = True Then Column2 * (Column5 - Column 6)
          When Expression3 = True Then Column3 * (Column6 - Column 7)
          Else
          Column4 - Column 5
      End
     )< 50

Now, we need to move our calculation to a CROSS APPLY section and we can refer to the calculated Column by its alias in the WHERE and in the GROUP BY clause as below:

SQL
Select 	Column1
	Column2, 
  	Calculated_Column
From
	Table
Cross Apply
  (
    Select 
      Calculated_Column = 
        
	Case 
          When Expression1 = True Then Column1 * (Column4 - Column 5)
          When Expression2 = True Then Column2 * (Column5 - Column 6)
          When Expression3 = True Then Column3 * (Column6 - Column 7)
          else Column4 - Column 5
        End
  ) Calculated_Column1Query
where Calculated_Column < 50

Hurray..!! We can also use a Derived table to accomplish the same as below:

SQL
;With Calculated_Column_Calculation As
(
  Select	Column1,
    		Column2, 
     		Calculated_Column = 
      		Case 
                 When Expression1 = True Then Column1 * (Column4 - Column 5)
                 When Expression2 = True Then Column2 * (Column5 - Column 6)
                 When Expression3 = True Then Column3 * (Column6 - Column 7)
          	Else
		  Column4 - Column 5
      		End
  From
		Table
)

Select 		* 
From
		Calculated_Column_Calculation
Where
		Calculated_Column < 50

DONE!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBit simpler way Pin
Henn Sarv15-Jun-13 5:48
Henn Sarv15-Jun-13 5:48 
AnswerRe: Bit simpler way Pin
martin.fay16-Jun-13 22:26
martin.fay16-Jun-13 22:26 
This may be marginally more terse, but I find the CTE syntax used in the article more readable (doesn't split the "outer query" in the middle).
GeneralMy vote of 5 Pin
Peni13-Jun-13 21:39
Peni13-Jun-13 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.