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