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
 Explanation

Author  Topic 

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-04-26 : 01:35:17
SUM(CASE WHEN custid = 'A' THEN qty END) AS A

could any explain abt the query i didn't understand i read in a book plz help i can't get it

KaShYaP

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-04-26 : 01:46:32
Post the full query!

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-04-26 : 01:47:25
SELECT empid,
SUM(CASE WHEN custid = 'A' THEN qty END) AS A,
SUM(CASE WHEN custid = 'B' THEN qty END) AS B,
SUM(CASE WHEN custid = 'C' THEN qty END) AS C,
SUM(CASE WHEN custid = 'D' THEN qty END) AS D
FROM dbo.Orders
GROUP BY empid


KaShYaP
Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-04-26 : 01:47:43
this is the full query

KaShYaP
Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-04-26 : 01:57:37
A B C D
1 NULL 20 34 NULL
2 52 27 NULL NULL
3 20 NULL 22 30

The output is this

KaShYaP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-26 : 01:58:00
its basically group the data based on empid then taking sum of qty field conditionally based on value of custid. So this will actually give you total qty value for each empid based on custod value in different columns

ie like

EmpID A B C D
1000 12 4 13 25
...

this is called cross tabbing

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

Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-04-26 : 02:12:28
i think another name is pivorting is it correct

KaShYaP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-26 : 02:21:15
yup..both are same...cross tabbing and pivoting

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

Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-04-26 : 02:24:16
ok tnks

KaShYaP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-26 : 03:49:22
welcome

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

Go to Top of Page
   

- Advertisement -