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
 case

Author  Topic 

gagani
Posting Yak Master

112 Posts

Posted - 2011-11-01 : 17:21:01
I have a table like:
Payroll taxcode type amount
1 101 A 1000
2 102 B 1000
3 103 C 2000
4 104 D 3000
5 105 E 4000
6 106 F 3000
7 107 G 2000
...............
Required output:
Amount in the type other than A, B, C should be added
output should be:

amount
1000
1000
2000
12000

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-11-01 : 18:36:38
can you explain how do you get the output ?


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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-02 : 05:16:54
[code]
SELECT SUM(Amount) AS Amount
FROM TAble
GROUP BY CASE WHEN type IN ('A','B','C') THEN type ELSE 'Other' END
[/code]

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

Go to Top of Page

gagani
Posting Yak Master

112 Posts

Posted - 2011-11-02 : 10:29:57
Thank you for your reply.
Is it possible for getting the output as
4000
12000

that is, adding types A,B,C as one and the rest combinedly as other.

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-02 : 10:34:03
pssible


SELECT SUM(Amount) AS Amount
FROM TAble
GROUP BY CASE WHEN type IN ('A','B','C') THEN 'ABC Group' ELSE 'Other' END


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

Go to Top of Page

gagani
Posting Yak Master

112 Posts

Posted - 2011-11-02 : 11:27:05
Thanks again and sorry to annoy.
I am working on a similar thing but with amount into two separate columns.

The actual query:
Select A.PayrollNo,SUM(B.Amount)as TaxExpense
FROM Table1 A With(NoLock), Table2 B With(NoLock) where
A.ID=B.ID

I am trying to split based on the condition into two separate columns for the amount rather than one after the other. so i wrote like this:

Select A.PayrollNo,SUM(case when B.type in('A','B','C') then
B.Amount else 0 end)as TaxExpense1,
SUM(case when B.type not in('A','B','C') then B.Amount else 0 end) as TaxExpense2
FROM Table1 A With(NoLock), Table2 B where
A.ID=B.ID

But somehow its not working. Could you please help with it.

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-02 : 11:42:36
[code]
Select A.PayrollNo,SUM(case when B.type in('A','B','C') then
B.Amount else 0 end)as TaxExpense1,
SUM(case when B.type not in('A','B','C') then B.Amount else 0 end) as TaxExpense2
FROM Table1 A With(NoLock), Table2 B where
A.ID=B.ID
group by A.PayrollNo
[/code]

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-02 : 11:42:57
also see

http://visakhm.blogspot.com/2010/02/avoiding-deadlocks-using-new.html

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

Go to Top of Page
   

- Advertisement -