Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Simple case query

Author  Topic 

KilpAr
Yak Posting Veteran

80 Posts

Posted - 2012-04-17 : 02:11:05
I would need to make this kind of query from SQL:

SELECT FieldF, FieldG,
ROUND(SUM(FieldA*FieldB*FieldC*(CASE WHEN FieldD = NULL THEN 1 ELSE FieldD END AS FieldE))) AS FieldH
FROM TableA
GROUP BY FieldF, FieldG
ORDER BY FieldF, FieldG

For some reason I can't the case part to work. I think the idea of it is pretty clear, make sure that if there's a null, you consider it as 1, otherwise use it "as is". Right now I make the query for Excel and need to make the multiplication there, but please tell me that there's no need to.

I have no previous experience of Cases in SQL, so I might do something totally wrong there.

Thanks in advance!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-04-17 : 02:11:59
CASE WHEN FieldD IS NULL


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-17 : 02:32:22
kh has given the answer


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

KilpAr
Yak Posting Veteran

80 Posts

Posted - 2012-04-17 : 02:41:52
Very good, now it works! Although I had to take the "AS FieldE" out as well.
Go to Top of Page
   

- Advertisement -