If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Error Using CASE statment in SQL Server 2008

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-31-10, 22:32
vr19 vr19 is offline
Registered User
 
Join Date: Aug 2010
Posts: 2
Error Using CASE statment in SQL Server 2008

Hello Everyone,

I have been trying to run the below query and keep getting the error message

Cannot perform an aggregate function on an expression containing an aggregate or a subquery

I think its got to do with the second case statement, but I'm unsure what I have done wrong.

Can someone also let me know if my Count syntax is correct ?



select
TransNumber,
SUM(CASE WHEN Transkey = '8870' THEN 1 ELSE 0 END) AS Value1,
SUM(CASE WHEN Transkey IN (select Buttonkey from Transaction_Keys) THEN '1' ELSE '0' END) as Value2
from TransDetails_201008
where Loc = 302
group by TransNumber having (Count (Value1) >0 and (COUNT(Value2)=0)


Any help is greatly appreciated.

thanks,
vr
Reply With Quote
  #2 (permalink)  
Old 09-01-10, 08:14
Wim Wim is online now
Registered User
 
Join Date: Nov 2004
Posts: 1,161
Not sure what you wanted to get from "SUM(CASE WHEN Transkey IN (select Buttonkey from Transaction_Keys) THEN '1' ELSE '0' END) as Value2". A long string of 1's and 0's? like a binary number? I consider it as a typo, and that you wanted to sum the numeric values 1 or 0.

The calculated columns Value1 and Value2 are not variables like in a programming language. If you want to do something with them in the HAVING clause, you will have to repeat the calculations.

"group by TransNumber having (Count (Value1) >0 and (COUNT(Value2)=0)" I guessed you wanted only those where the column Value1 > 0 and the column Value2 equals 0.

Try this:
Code:
select	TransNumber,
	SUM(CASE WHEN Transkey = '8870' THEN 1 ELSE 0 END) AS Value1
	,SUM(CASE WHEN Buttonkey IS NOT NULL THEN 1 ELSE 0 END) as Value2
from TransDetails_201008
	LEFT OUTER JOIN Transaction_Keys ON
		Transkey = Buttonkey
where Loc = 302
group by TransNumber 
having SUM(CASE WHEN Transkey = '8870' THEN 1 ELSE 0 END) > 0 and 
	SUM(CASE WHEN Buttonkey IS NOT NULL THEN 1 ELSE 0 END) = 0
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 13
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #3 (permalink)  
Old 09-01-10, 10:45
vr19 vr19 is offline
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks for the response Wim.

For the below Case statement

SUM(CASE WHEN Transkey IN (select Buttonkey from Transaction_Keys) THEN '1' ELSE '0' END) as Value2

I wanted to show 1 if the Transkey matched any of Button keys from the Transaction_Keys table else I wanted to be show as a 0 .
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On