SQL查询问题,GROUP BY a之后,找存在b=1,2,3,A,B的a
说的不是很清楚:如图,GCODE拥有不同的状态(MCODE),比如现在已知MCODE必有(0,1,3),查出符合这个条件的GCODE,观察可知满足该条件的结果是GCODE...
说的不是很清楚:如图,GCODE 拥有不同的状态(MCODE),比如现在已知MCODE必有(0,1,3),查出符合这个条件的GCODE,观察可知满足该条件的结果是GCODE=WORULE_WOWAY,请问怎么写SELECT语句呢?
展开
1个回答
2017-08-17
展开全部
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询