create table t
(
id int,
a int,
b int
)
insert into t values(1,101,1)
insert into t values(2,102,1)
insert into t values(3,101,2)
insert into t values(4,102,2)
insert into t values(5,101,1)
insert into t values(6,102,3)
insert into t values(7,102,3)
--先把T表的a,b做个笛卡尔积,在跟t表做left,然后统计
select a.a,b.b,COUNT(c.id) As 个数 from
(select distinct a from t) a cross join
(select distinct b from t) b
left join t c on a.a=c.a and b.b=c.b
Group by a.a,b.b