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 |
|
Teachme
Starting Member
45 Posts |
Posted - 2006-11-03 : 10:52:17
|
| Hi if i have 2 tables(tbl A, tbl B). Tbl A has 2 columns (account,cash) and it has only one record. Tbl B has 3 columns(account,deposits,transactionCode) as well. They are joined by account and it has 20 records. In my view i wana retrieve everything from tblA which is only one record and from tblb i wana retrieve everything as well but i wana split deposits column into two columns as deposits_one and deposits_two based on transactionCodes but i dont want the cash from tblA to be repeated because i have to sum them at the end. my data is something like thisSampletbl Aaccount cash123 100tblbaccount deposits trnansactionCode123 50 5123 75 10123 200 5123 80 10123 90 5The result should look something like thisaccount cash deposits1 deposits2123 100 340(sum of deposits where code is 5) 155(for code 10)how do i go about getting something like this. Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-03 : 11:02:48
|
[code]select a.accont, a.cash, deposits1 = sum(case when trnansactionCode = 5 then deposits else 0 end), deposits2 = sum(case when trnansactionCode = 10 then deposits else 0 end)from tbla a inner join tblb b on a.account = b.accountgroup by a.accont, a.cash[/code] KH |
 |
|
|
Teachme
Starting Member
45 Posts |
Posted - 2006-11-03 : 11:30:59
|
| well this query doesnt give me the result im after. caz i have to do a sum on a.cash as well if i do tat it would give me 500 for cash which is not right. Is there any other way around this. because if just do a select with no sums it puts 100 for cash in each record repeated 5 times if somehow it just gives 100 once then i can sum it up and i wud get wat i want..thanks |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2006-11-03 : 12:20:35
|
| Why do need to sum a.cash? It is part of the grouping?Jay White |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-03 : 12:45:47
|
quote: Originally posted by Teachme well this query doesnt give me the result im after. caz i have to do a sum on a.cash as well if i do tat it would give me 500 for cash which is not right. Is there any other way around this. because if just do a select with no sums it puts 100 for cash in each record repeated 5 times if somehow it just gives 100 once then i can sum it up and i wud get wat i want..thanks
What do you want actually ?Post more sample data and the expected result ! KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-03 : 13:02:00
|
| select a.accont, a.cash, sum(b.deposits), deposits1 = sum(case when trnansactionCode = 5 then deposits else 0 end), deposits2 = sum(case when trnansactionCode = 10 then deposits else 0 end)from tbla a inner join tblb b on a.account = b.accountgroup by a.accont, a.cashPeter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|