I have below scenario, Where I want to fetch all distinct records from first table & matching records from second & for non matching show 0,
Table1
id group
1 a
2 b
3 c
4 a
5 b
Table1 having group data,
Table2
m_id group Available Flag
1 a 100 A
2 a 200 A
2 b 100 A
3 b 150 A
3 c 280 A
4 a -50 D
4 b 20 D
Table2 having items data available by group wise,
I want groups list with items available with Flag=A or not, Desired output,
m_id group Available
1 a 100
1 b 0
1 c 0
2 a 200
2 b 100
2 c 0
3 a 0
3 b 150
3 c 280
I have tried this through left join but It not gives desired output.
select t2.M_ID,t1.GROUP,t2.Available
from #temp as t1
left join #temp2 as t2 on t1.GROUP=t2.GROUP AND t2.flag='A'
group by t2.M_ID,t1.GROUP,t2.Available
Output is,
M_ID GROUP Available
1 a 100
2 a 200
2 b 100
3 c 280
3 b 150
Please suggest me for desired output.