mysql - Join and Group by Clause gives wrong output -
i have 2 table l columns (code, qtr, fy, limit) , r (code, qtr,fy,limit). want sum of limit of left , right table group code, qtr fy
the following query runs no error gives wrong output, can me in getting right output. if use 1 table works fine. guess problem join
select l.code, l. qtr, l.fy, sum(l.limit),sum(r.limit) tbl l,tbl r l.code=r.code , l.qtr=r.qtr , l.fy=r.fy group l.code,l.qtr,l.fy
sample data ( table contains other column here m keeping selected)
tbl l code qtr, fy limit 001 1 70 200 001 1 70 700 001 2 70 500 001 2 70 300 table r code qtr fy limit 001 1 70 1000 001 1 70 200 001 2 70 50 001 2 70 125 result code qtr fy sum(l.limit) sum(r.limit) 001 1 70 900 1200 001 2 70 800 175
i m using mysql
try query:
select code, qtr, fy, sum(lsum), sum(rsum) ( select l.code, l.qtr, l.fy, l.limit lsum, 0 rsum l union select r.code, r.qtr, r.fy, 0 lsum, r.limit rsum r) combined group code, qtr, fy
using join
in case wrong idea because create multiple records (one each match between l , r) , when sum
incorrect results.
Comments
Post a Comment