各位大大,请教一个sql

[复制链接]
查看11 | 回复9 | 2016-1-6 14:01:09 | 显示全部楼层 |阅读模式
求教各位了,我是新手,我有一个表table_1大概1000条数据
有column1到column3和ID这4个字段,数据举例如下
COLUMN1COLUMN2
COLUMN3 ID
---------- ------------------------------ -------
-----
aaabbb
ccc 1
aaabbb
ccc 2
asdf bbb
ffff 3
aaabbb
ccc 4
abcbcd
sda 5
aaabbb
ddd 6
cccbbb
ccc 7
现在就想把column1,column2,column3都相同的记录通过一条sql查询出来,就比如:aaa bbb ccc这些列,有什么好办法吗?

回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
板砖
select t.* from table t1,
(select col1,col2,col3,count(1) from table t2
group by col1,col2,col3
having count(1) > 1) t3
where t1.col1 = t3.col1
and t1.col2 = t3.col2
and t1.col3 = t3.col3
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层

如下:
SQL> with tmp as
2(
3select 'aaa' column1, 'bbb' column2, 'ccc' column3, 1 id from dual
4union all
5select 'aaa' column1, 'bbb' column2, 'ccc' column3, 2 id from dual
6union all
7select 'asdf' column1, 'bbb' column2, 'ffff' column3, 3 id from dual
8union all
9select 'aaa' column1, 'bbb' column2, 'ccc' column3, 4 id from dual
10union all
11select 'abc' column1, 'bcd' column2, 'sda' column3, 5 id from dual
12union all
13select 'aaa' column1, 'bbb' column2, 'ddd' column3, 6 id from dual
14union all
15select 'ccc' column1, 'bbb' column2, 'ccc' column3, 7 id from dual
16)
17selectcolumn1,
18
column2,
19
column3,
20
id
21from
22(select column1,
23
column2,
24
column3,
25
id,
26
count(*) over(partition by column1,column2,column3) cnt
27from tmp)
28where cnt > 1;
COLUMN1 COLUMN2 COLUMN3 ID
------- ------- ------- ----------
aaa bbb ccc
1
aaa bbb ccc
2
aaa bbb ccc
4
SQL>

回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
bell6248 发表于 2012-1-9 10:51
如下:
SQL> with tmp as

版主正解!
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
多谢visual2006兄,我大概理解了一下思路,就是先生成一个中间表这个表里面包含的只有aaa,bbb,ccc然后通过与这个表的字段关联得到结果
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
bell的更好,只需扫描一次
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
bell6248 发表于 2012-1-9 10:51
如下:
SQL> with tmp as

多谢版主,with这个方式第一次接触,不用建表了,这么方便
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
with是功能挺强大的,并且查询出来的还放在内存中,效率更高,11G还支持递归、分层····
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
bell6248 发表于 2012-1-9 10:51
如下:
SQL> with tmp as

请问bell:如果我写这句多一个where条件为什么就报错不能执行呢?
select column1,

column2,

column3,

id,

count(*) over(partition by column1,column2,column3) cnt
from tmp where cnt >1
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
周应侯 发表于 2012-1-9 13:17
with是功能挺强大的,并且查询出来的还放在内存中,效率更高,11G还支持递归、分层····

在内存中?原来如此
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行