Reputation: 67
Requirements: I want to show the stock for particular B_Name, if date are same it will show on same row, if date are not same it will show on different row and also '-' will be specify
Two tables namely gr and frosted
table gr:
B_Name B_Date B_Qty
A 2015-08-11 15000
A 2015-08-15 25000
A 2015-08-31 20000
table frosted:
M_Name M_Date M_Qty
A 2015-08-11 15000
A 2015-08-25 25000
B 2015-08-20 20000
A 2015-08-15 15000
Following output:
for Particular B_Name like here (A)
Date Inward(B_Qty) Outward(M_Qty)
2015-08-11 15000 15000
2015-08-15 25000 15000
2015-08-31 20000 -
2015-08-25 - 25000
I tried join and union but it is not working as per my requirements.
Upvotes: 0
Views: 88
Reputation: 26
I have tried and found the required result:
SELECT table1.bdate as "Date",coalesce(t1.B_Qty,'-') as "Inward(B_Qty)",coalesce(t2.M_Qty,'-') as "Outward(M_Qty)" FROM ( select B_date as bdate,B_Name as bname from gr union select M_Date as mdate,M_Name as mname from frosted) as table1 LEFT JOIN gr as t1 ON t1.B_Date = table1.bdate LEFT JOIN frosted as t2 ON t2.M_Date = table1.bdate WHERE table1.bname like 'A'
Upvotes: 0
Reputation: 94914
You want records from table gr that have no match in table frosted and vice versa. So you need a full outer join, which MySQL doesn't support.
A possible workaround: Get all dates with a union query first, then outer join the tables again.
select
alldates.value as "Date",
coalesce(g.b_qty, '-') as "Inward(B_Qty)",
coalesce(f.m_qty, '-') as "Outward(B_Qty)"
from
(
select b_date as value from gr
where b_name = 'A'
union
select m_date from frosted
where m_name = 'A'
) all_dates
left join gr g on g.b_date = all_dates.value
and g.b_name = 'A'
left join frosted f on f.m_date = all_dates.value
and f.m_name = 'A'
Upvotes: 1