| Author |
Topic |
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-11-01 : 17:21:01
|
| I have a table like:Payroll taxcode type amount1 101 A 1000 2 102 B 10003 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:amount10001000200012000 |
|
|
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] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-02 : 05:16:54
|
| [code]SELECT SUM(Amount) AS AmountFROM TAbleGROUP BY CASE WHEN type IN ('A','B','C') THEN type ELSE 'Other' END[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 400012000that is, adding types A,B,C as one and the rest combinedly as other.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-02 : 10:34:03
|
pssibleSELECT SUM(Amount) AS AmountFROM TAbleGROUP BY CASE WHEN type IN ('A','B','C') THEN 'ABC Group' ELSE 'Other' END------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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) whereA.ID=B.IDI 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 TaxExpense2FROM Table1 A With(NoLock), Table2 B whereA.ID=B.IDBut somehow its not working. Could you please help with it.Thanks |
 |
|
|
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 TaxExpense2FROM Table1 A With(NoLock), Table2 B whereA.ID=B.IDgroup by A.PayrollNo[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|