问个sql基础问题

amdgaming 2010-12-03 03:29:42
表 结构 id code type 依次是number,varchar,number类型
如何 查出 code 一样 type 为 1 的 记录 大于2
比如 1, 003 ,1
2, 003 ,1
3, 004,2
4, 005,3
5, 006,1
6, 006,1

这里 应该 是 003
006
...全文
87 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
手撕煎饼 2010-12-03
  • 打赏
  • 举报
回复
不太明白
心中的彩虹 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 amdgaming 的回复:]
表 结构 id code type 依次是number,varchar,number类型
如何 查出 code 一样 type 为 1 的 记录 大于2
比如 1, 003 ,1
2, 003 ,1
3, 004,2
4, 005,3
5, 006,1
6, 006,1

这里 应该 是 003
006
[/Quote]


select distinct code
from tb a
where a.type=1 and (select count(id) from tb b where a.type=b.type)>2


amdgaming 2010-12-03
  • 打赏
  • 举报
回复
谢谢 各位啊,看来一次能写对 不那么容易啊
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 java3344520 的回复:]
SQL code
select id,code
from tab
where type='1'
group by code
having count(*)>2


这样。。
[/Quote]

--偷学下杨哥的
已写入 file afiedt.buf

1 with tb as(
2 select 1 id,'003' code,1 type from dual union all
3 select 2,'003',1 from dual union all
4 select 3,'004',2 from dual union all
5 select 4,'005',3 from dual union all
6 select 5,'006',1 from dual union all
7 select 6,'006',1 from dual)
8 select code
9 from tb
10 where type=1
11 group by code,type
12* having count(id)>=2
SQL> /

COD
---
003
006

  • 打赏
  • 举报
回复

with tb as(
select 1 id,'003' code,1 type from dual union all
select 2,'003',1 from dual union all
select 3,'004',2 from dual union all
select 4,'005',3 from dual union all
select 5,'006',1 from dual union all
select 6,'006',1 from dual)
--以上为提供数据的语句
select code
from (select code,type,count(id) cnt from tb group by code,type)
where cnt>=2

COD
---
003
006
iqlife 2010-12-03
  • 打赏
  • 举报
回复
select id,code
from tab
where type='1'
group by code
having count(*)>2


这样。。
iqlife 2010-12-03
  • 打赏
  • 举报
回复
select id,code
from tab
where type='1'
group by id
having count(*)>=2

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧