SQL查询问题,GROUP BY a之后,找存在b=1,2,3,A,B的a
答案:1 悬赏:0 手机版
解决时间 2021-04-26 12:22
- 提问者网友:城市野鹿
- 2021-04-26 02:34
SQL查询问题,GROUP BY a之后,找存在b=1,2,3,A,B的a
最佳答案
- 五星知识达人网友:污到你湿
- 2021-04-26 03:42
SQL查询问题,GROUP BY a之后,找存在b=1,2,3,A,B的a
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
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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯