I have 3 tables with same columns, I need to get count of rows with respect to packname column for all the 3 tables.
Below are the input and output table data.
TABLE 1
CALLERID PACKANME
9882000342 DOC
9882000560 DOC
9882000945 RAJ
9882002655 CAR
9882003225 CAR
TABLE 2
CALLERID PACKANME
9882000342 DOC
9882000560 DOC
9882000945 RAJ
9882002655 CAR
TABLE 3
CALLERID PACKANME
9882000342 DOC
9882000560 DOC
9882000945 RAJ
OUTPUT
PACKNAME COUNT TAB2 COUNT TAB3
DOC 2 2
RAJ 1 1
CAR 1 0
I have tried below query and stopped here, I beleive we have use pivot to get the desired output but unable to do that.Please help me out.
Select 'TableA' as 'TableName',tbla.packname as Ptype,Count(tbla.num) as MobCnt from tbla
Group By tbla.packname
Union all
Select 'TableB' as 'TableName',tblb.packname as Ptype,Count(tblb.num)as MobCnt from tblb
Group By tblb.packname
Union all
Select 'TableC' as 'TableName',tblc.packname as Ptype,Count(tblc.num)as MobCnt from tblc
Group By tblc.packname
First off, generate a list of distinct packnames across all tables. Then outer join a count of each table onto this. This way, you can read zero counts correctly.