我有一个表resTable中有四个字段ResID,NodeID,PublisherID,MediumID
其中ResID和NodeID共同构成主健,现在我要统计的是
每一种(去除重复)PublisherID 对应的每一种MediumID的ResID的个数
如:表中有三种PublisherID,三种MediumID则应返回如下:
PublisheriD MediumID CountS
P01 M01 N1
P01 M02 N2
P01 M03 N3
P02 M01 N4
P02 M02 N5
P02 M03 N6
P03 M01 N7
P03 M02 N8
P03 M03 N9
如果某一项(假设P01,M02,N2)缺失的话,也有返回(P01,M02 ,0)一个空项
谢谢!
---------------------------------------------------------------
select
c.PublisheriD,
c.MediumID,
sum(case when d.MediumID is null then 0 else 1 end)
from
(select * from
(select distinct PublisheriD from resTable) a,
(select distinct MediumID from resTable) b
) c left join resTable d
on c.PublisheriD=d.PublisheriD and c.MediumID=d.MediumID
group by c.PublisheriD,c.MediumID