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.
| Author |
Topic |
|
sajanjacobk
Starting Member
7 Posts |
Posted - 2009-07-24 : 02:24:33
|
| I am using composite primary key.Number SequenceNo CreditAmount DebitAmount200 1 100.00 10.00200 2 100.00 20.00200 3 400.00 10.00201 1 200.00 10.00201 2 300.00 10.00201 3 210.25 10.00I need to get the sum(CreditAmount) and sum(DebitAmount) against each number.along with Number, SequenceNo, CreditAmount and DebitAmount.the result I needNumber SequenceNo CreditAmount DebitAmount SUM(CreditAmount) SUM(DebitAmount)200 1 100.00 10.00 700.00 40.00200 2 100.00 20.00 700.00 40.00200 3 400.00 10.00 700.00 40.00201 1 200.00 10.00 710.25 30.00201 2 300.00 10.00 710.25 30.00201 3 210.25 10.00 710.25 30.00Please help thanks in advance. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 02:34:01
|
Are you using SQL 2000 or 2005/2008 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-07-24 : 02:39:49
|
| Hey Try thisselect a.*,tab.number,tab.sum_credit,tab.sum_debit fromamount a inner join (select number,sum(credit) as sum_credit,sum(debit) as sum_debit from amount group by number)as tab on a.number=tab.numberSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 02:40:15
|
[code]DECLARE @sample TABLE( Number int, SequenceNo int, CreditAmount decimal(5,2), DebitAmount decimal(5,2))INSERT INTO @sampleSELECT 200, 1, 100.00, 10.00 UNION ALLSELECT 200, 2, 100.00, 20.00 UNION ALLSELECT 200, 3, 400.00, 10.00 UNION ALLSELECT 201, 1, 200.00, 10.00 UNION ALLSELECT 201, 2, 300.00, 10.00 UNION ALLSELECT 201, 3, 210.25, 10.00-- SQL 2005/2008SELECT Number, SequenceNo, CreditAmount, DebitAmount, TotalCredit = SUM(CreditAmount) OVER (PARTITION BY Number), TotalDebit = SUM(DebitAmount) OVER (PARTITION BY Number)FROM @sample-- SQL 2000SELECT s.Number, s.SequenceNo, s.CreditAmount, s.DebitAmount, t.TotalCredit, t.TotalDebitFROM @sample s INNER JOIN ( SELECT Number, TotalCredit = SUM(CreditAmount), TotalDebit = SUM(DebitAmount) FROM @sample GROUP BY Number ) t ON s.Number = t.Number /*Number SequenceNo CreditAmount DebitAmount TotalCredit TotalDebit ----------- ----------- ------------ ----------- ------------ ------------200 1 100.00 10.00 600.00 40.00200 2 100.00 20.00 600.00 40.00200 3 400.00 10.00 600.00 40.00201 1 200.00 10.00 710.25 30.00201 2 300.00 10.00 710.25 30.00201 3 210.25 10.00 710.25 30.00(6 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-07-24 : 02:43:40
|
| try thisdeclare @temp table ( Number int, SequenceNo int, CreditAmount numeric(18,2), DebitAmount numeric(18,2) )insert into @tempselect 200, 1, 100.00, 10.00 union allselect 200, 2, 100.00, 20.00 union allselect 200, 3, 400.00, 10.00 union allselect 201, 1, 200.00, 10.00 union allselect 201, 2, 300.00, 10.00 union allselect 201, 3, 210.25, 10.00--select * from @tempselect t1.number,t1.sequenceno,t1.CreditAmount, t1.DebitAmount,t2.credittotal as 'sum(creiditamount)',t2.debittotal as 'sum(DebitAmount)'from @temp t1inner join ( select number,sum(CreditAmount) as credittotal,sum(DebitAmount) as debittotal from @temp group by number ) t2 on t2.number = t1.number |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-24 : 05:07:04
|
quote: Originally posted by sajanjacobk I am using composite primary key./*Number SequenceNo CreditAmount DebitAmount200 1 100.00 10.00200 2 100.00 20.00200 3 400.00 10.00201 1 200.00 10.00201 2 300.00 10.00201 3 210.25 10.00*/I need to get the sum(CreditAmount) and sum(DebitAmount) against each number.along with Number, SequenceNo, CreditAmount and DebitAmount.the result I need/*Number SequenceNo CreditAmount DebitAmount SUM(CreditAmount) SUM(DebitAmount)200 1 100.00 10.00 600.00 40.00200 2 100.00 20.00 600.00 40.00200 3 400.00 10.00 600.00 40.00201 1 200.00 10.00 710.25 30.00201 2 300.00 10.00 710.25 30.00201 3 210.25 10.00 710.25 30.00*/Please help thanks in advance.
and u will get 600 for number 200 in creditamount check it once.use any one query for required output. |
 |
|
|
|
|
|
|
|