当前位置: 代码迷 >> Sql Server >> 小弟我想查询出以A,B列分组后对应的B列所有值大于2的A列值,即结果只有是3的查询语句
  详细解决方案

小弟我想查询出以A,B列分组后对应的B列所有值大于2的A列值,即结果只有是3的查询语句

热度:21   发布时间:2016-04-27 11:41:04.0
我想查询出以A,B列分组后对应的B列所有值大于2的A列值,即结果只有是3的查询语句
查询语句
A列 B列
1 1
1 2
1 3
2 1
2 2
3 3
3 4

我想查询出以A,B列分组后对应的B列所有值大于2的A列值,即结果只有是3的查询语句

------解决方案--------------------
如果只有两列
select distinct a列 from tb where b列>2

有多列也同上面一样 你只查a列

------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([A] int,[B] int)insert [tb]select 1,1 union allselect 1,2 union allselect 1,3 union allselect 2,1 union allselect 2,2 union allselect 3,3 union allselect 3,4goselect * from tb twhere not exists(select 1 from tb where a=t.a and b<=2)/**A           B----------- -----------3           33           4(2 行受影响)**/
------解决方案--------------------
探讨
select * from tb t
where not exists(select 1 from tb where a=t.a and b<=2)
请教兄台
select 1 from tb where a=t.a and b<=2是什么意思
  相关解决方案