Author |
Topic |
didin
Starting Member
1 Post |
Posted - 2008-05-23 : 09:35:18
|
Hi all,I'm trying to joining three tables belowT1str_no | pos_no | trn_no | grossale | discount001 | 001 | 00001 | 50 | 5001 | 001 | 00001 | 30 | 0001 | 002 | 00002 | 20 | 0001 | 003 | 00003 | 60 | 10....T2str_no | pos_no | trn_no | grossale | discount001 | 001 | 00001 | 80 | 5001 | 002 | 00002 | 20 | 0001 | 003 | 00003 | 60 | 10....T3str_no | pos_no | trn_no | payment001 | 001 | 00001 | 45001 | 001 | 00001 | 30001 | 002 | 00002 | 20001 | 003 | 00003 | 60....using this queryselect T1.str_no,T1.pos_no,sum(T1.grossale-discount) as Netto1,sum(T2.grossale-discount) as Netto2,sum(T3.payment) as Paymentfrom T1left outer join T2 on (T1.trn_no=T2.trn_no)left outer join T3 on (T2.trn_no=T3.trn_no)group by T1.str_no,T1.pos_noorder by T1.str_no,T1.pos_noI wanna view result of those query :str_no | pos_no | Netto1 | Netto2 | Payment001 001 75 75 75001 002 20 20 20...But, the problem is result between Netto1,Netto2 and Payment are differentPlease tell me what's wrong |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 10:01:16
|
[code]DECLARE @t1 TABLE (str_no INT, pos_no INT, trn_no INT, grossale INT, discount INT)INSERT @t1SELECT 001 , 001 , 00001 , 50 , 5 UNION ALLSELECT 001 , 001 , 00001 , 30 , 0 UNION ALLSELECT 001 , 002 , 00002 , 20 , 0 UNION ALLSELECT 001 , 003 , 00003 , 60 , 10DECLARE @t2 TABLE (str_no INT, pos_no INT, trn_no INT, grossale INT, discount INT)INSERT @t2SELECT 001 , 001 , 00001 , 80 , 5 UNION ALLSELECT 001 , 002 , 00002 , 20 , 0 UNION ALLSELECT 001 , 003 , 00003 , 60 , 10DECLARE @t3 TABLE (str_no INT, pos_no INT, trn_no INT, payment INT)INSERT @t3SELECT 001 , 001 , 00001 , 45 UNION ALLSELECT 001 , 001 , 00001 , 30 UNION ALLSELECT 001 , 002 , 00002 , 20 UNION ALLSELECT 001 , 003 , 00003 , 60select T1.str_no, T1.pos_no, sum(T1.grossale - t1.discount) as Netto1, T2.Netto2, T3.paymentfrom @T1 as t1left join ( select trn_no, sum(grossale - discount) AS netto2 from @t2 group by trn_no ) as t2 on T2.trn_no = T1.trn_noleft join ( select trn_no, sum(payment) AS payment from @t3 group by trn_no ) AS T3 on T3.trn_no = T2.trn_nogroup by T1.str_no, T1.pos_no, t2.netto2, t3.paymentorder by T1.str_no, T1.pos_no[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|