| Author |
Topic |
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-12-05 : 05:34:44
|
| Hello All, I have 2 tables Trans & Slmast In Trans Tran_type,Tran_amt,Slcode---------------------------- VES,10000.00,AB01 DBN,20000.00,AB01 CRN,30000.00,AB01 BRT,40000.00,AB01 DBN,10000.00,AB02 CRN,50000.00,AB02 BRT,20000.00,AB03 CRN,30000.00,AB03In Slmast Slcode,Sldesc-------------- AB01,ABC Ltd AB02,CAB Ltd AB03,BAC LtdAll transaction related datas will be in Trans for which Slmast is the master.I want to sum those records based on slcode,tran_type for slcode which does not have tran_type with 'VES'Sample Output Slcode,Sldesc,DBN,CRN,BRT AB02,CAB Ltd,10000,50000,0 AB03,BAC Ltd,0,30000,20000 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-05 : 05:41:21
|
| [code]Select sm.SlCode, sm.SlDesc,Sum(Case when t.Tran_Type = 'DBN' then Tran_Amt else 0.00 end) as DBN,...From Trans t JOIN Slmast smon t.SlCode = sm.SlCodegroup by sm.SlCode, sm.SlDesc[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-12-05 : 05:44:47
|
| Hello Harsh, I want to sum records which does not have Tran_type with VESNirene |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-05 : 05:48:55
|
| So what?Filter it by adding WHERE condition.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
nirene
Yak Posting Veteran
98 Posts |
Posted - 2007-12-05 : 05:52:17
|
| Hello Harsh, I want the result for only AB02 & AB03 not AB01 cos it has a record with Tran_type with VES.Nirene |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-05 : 06:01:13
|
| [code]Select sm.SlCode, sm.SlDesc,Sum(Case when t.Tran_Type = 'DBN' then Tran_Amt else 0.00 end) as DBN,...From Trans t JOIN (Select Distinct slm.SlCode, slm.SlDesc from Slmast slm JOIN Trans trOn slm.SlCode = tr.SlCodeWhere tr.Tran_Type <> 'VES') smon t.SlCode = sm.SlCodegroup by sm.SlCode, sm.SlDesc[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-05 : 06:20:20
|
[code]-- Prepare sample dataDECLARE @Trans TABLE (TranType VARCHAR(3), TranAmt MONEY, SlCode VARCHAR(4))INSERT @TransSELECT 'VES', 10000.00, 'AB01' UNION ALLSELECT 'DBN', 20000.00, 'AB01' UNION ALLSELECT 'CRN', 30000.00, 'AB01' UNION ALLSELECT 'BRT', 40000.00, 'AB01' UNION ALLSELECT 'DBN', 10000.00, 'AB02' UNION ALLSELECT 'CRN', 50000.00, 'AB02' UNION ALLSELECT 'BRT', 20000.00, 'AB03' UNION ALLSELECT 'CRN', 30000.00, 'AB03'DECLARE @Master TABLE (SlCode VARCHAR(4), SlDesc VARCHAR(19))INSERT @MasterSELECT 'AB01', 'ABC Ltd' UNION ALLSELECT 'AB02', 'CAB Ltd' UNION ALLSELECT 'AB03', 'BAC Ltd'-- Show the expected outputSELECT t.SlCode, m.SlDesc, SUM(CASE WHEN t.TranType = 'DBN' THEN TranAmt ELSE 0 END) AS DBN, SUM(CASE WHEN t.TranType = 'CRN' THEN TranAmt ELSE 0 END) AS CRN, SUM(CASE WHEN t.TranType = 'BRT' THEN TranAmt ELSE 0 END) AS BRTFROM @Trans AS tINNER JOIN @Master AS m ON m.SlCode = t.SlCodeWHERE NOT EXISTS (SELECT * FROM @Trans AS x WHERE x.TranType = 'VES' AND x.SlCode = t.SlCode)GROUP BY t.SlCode, m.SlDescORDER BY t.SlCode[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-12-05 : 06:41:38
|
| See, Peso understood right away."Give me the full answer, not a push in the right direction where i might actually learn something"[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|
|
|