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
 General SQL Server Forums
 New to SQL Server Programming
 Getting sum from SubQueries

Author  Topic 

Gudiya
Starting Member

14 Posts

Posted - 2012-08-06 : 20:58:19
I have to the following case statement that calculates count and sum respectively:

if colA='e' or (cola='f' and colb<>0) and date=2012
then set xy=1
else set xy= o

if colA='e' or (cola='f' and colb<>0) and date=2012
then sum y
else o

here, I am joining 3 tables.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-06 : 21:37:49
use case when statement

http://msdn.microsoft.com/en-us/library/ms181765.aspx


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

Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2012-08-07 : 03:38:26
Can you please elobrate your requirement more clearly..?
quote:
Originally posted by Gudiya

I have to the following case statement that calculates count and sum respectively:

if colA='e' or (cola='f' and colb<>0) and date=2012
then set xy=1
else set xy= o

if colA='e' or (cola='f' and colb<>0) and date=2012
then sum y
else o

here, I am joining 3 tables.




------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-07 : 09:51:27
just translate it to case ...when

..
case when colA='e' or (cola='f' and colb<>0) and date=2012
then 1
else 0
end as xy,
case when colA='e' or (cola='f' and colb<>0) and date=2012
then sum(y)
else 0
end as othercol
..


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -