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 2005 Forums
 Transact-SQL (2005)
 tricky query

Author  Topic 

laailalalaa
Yak Posting Veteran

57 Posts

Posted - 2010-04-12 : 02:10:32
i have table1(accountno, operationtype, amountofmoney,date),where operationtype = A or B. i need to write a select stmnt that displays:
account, sum of amountofmoney for operations of type A, sum of amountofmoney for operations of type B for the account.

any ideas on how to proceed?
thank u

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-12 : 02:25:42
you can try this:

SELECT SUM(CASE WHEN Column2 = 'Some Value' THEN Column1 ELSE 0 END) as FieldNeeded1,

here in your example you can take :

SELECT accountno
,SUM(CASE WHEN operationtype = 'A' THEN amountofmoney ELSE 0 END) as amtA
,SUM(CASE WHEN operationtype = 'B' THEN amountofmoney ELSE 0 END) as amtB
FROM .....

Hope this helps....
Go to Top of Page

laailalalaa
Yak Posting Veteran

57 Posts

Posted - 2010-04-12 : 03:21:39
thanks, that worked.. and was awesome :D

quote:
Originally posted by mrm23

you can try this:

SELECT SUM(CASE WHEN Column2 = 'Some Value' THEN Column1 ELSE 0 END) as FieldNeeded1,

here in your example you can take :

SELECT accountno
,SUM(CASE WHEN operationtype = 'A' THEN amountofmoney ELSE 0 END) as amtA
,SUM(CASE WHEN operationtype = 'B' THEN amountofmoney ELSE 0 END) as amtB
FROM .....

Hope this helps....

Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-13 : 01:24:44
you are welcome :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-13 : 03:03:22
If the values are dynamic, use
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

laailalalaa
Yak Posting Veteran

57 Posts

Posted - 2010-04-27 : 11:15:56
yep, pivoting is an option as well, thanks madhivanan

quote:
Originally posted by madhivanan

If the values are dynamic, use
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 11:19:00
isnt this sorted yet?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=143556

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

Go to Top of Page

laailalalaa
Yak Posting Veteran

57 Posts

Posted - 2010-04-27 : 11:41:18
that's a completely different topic (even though table schemas are somewhat resembling).

quote:
Originally posted by visakh16

isnt this sorted yet?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=143556

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



Go to Top of Page
   

- Advertisement -