您的位置:程序门 -> oracle -> 开发



请教一个单表查询的sql


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教一个单表查询的sql
发表于:2007-03-05 16:00:47 楼主
表test:
col1       col2       col3
a             a             1
a             a             2
b             a             3
b             a             4
c             a             5
c             a             6
a             b             7
a             b             8
a             a             9
a             a             10
b             b             11
d             c             12
d             c             13
e             c             14
e             c             15
说明:表中有2种数据,第一种是col1=col2,第二种是col1!=col2,其中第二种数据又分两种情况,a情况是col2相同的数据具有相同的col1,b情况是col2相同的数据具有不同的col1。现在要求把b情况的数据中,只保留一种相同的col1即可(哪种col1任意,可以取min(col1)或其他方法),即如下结果:
col1       col2       col3
a             a             1
a             a             2
a             b             7
a             b             8
a             a             9
a             a             10
b             b             11
d             c             12
d             c             13
谢谢!

发表于:2007-03-05 16:03:011楼 得分:0
更正,不需考虑col1=col2的情况。即
表中有2种数据,a情况是col2相同的数据具有相同的col1,b情况是col2相同的数据具有不同的col1。现在要求把b情况的数据中,只保留一种相同的col1即可(哪种col1任意,可以取min(col1)或其他方法)
发表于:2007-03-05 16:14:072楼 得分:0

select   col1,col2,col3   from   test   where   col1 <> col2
发表于:2007-03-05 16:29:483楼 得分:0
有人知道正确方法吗
发表于:2007-03-05 16:40:394楼 得分:0
算了,揭帖
发表于:2007-03-05 16:46:365楼 得分:0
==
发表于:2007-03-05 16:58:006楼 得分:0
select   *   from   a   where   col1 <> col2   and   col1=(select   min(b.col1)   from   a   b   where   b.col2=a.col2   group   by   col2)

大致是这个思路     没有测试   自己去实验下吧
发表于:2007-03-05 17:01:347楼 得分:0
select   t1.*
from   test   t1,

select   col1,col2
from(
select   col1,   col2,   row_number()   over   (partition   by   col2   order   by   col2)   cnt   from   test   t1
group   by   col1,   col2  
order   by   col2
)   tt1
where   tt1.cnt=1
)   t2
where   t1.col1=t2.col1   and   t1.col2=t2.col2
发表于:2007-03-05 17:02:508楼 得分:0
select   t1.*
from   test   t1,

select   col1,col2
from(
select   col1,   col2,   row_number()   over   (partition   by   col2   order   by   col2)   cnt   from   test   t1
group   by   col1,   col2  
order   by   col2
)   tt1
where   tt1.cnt=1
)   t2
where   t1.col1=t2.col1   and   t1.col2=t2.col2   and   t1.col1 <> t2.col2
---------------------------------------------------------------
写得有点烦,但是可以用
发表于:2007-03-05 17:07:389楼 得分:0
多谢上面2位,我再研究一下
发表于:2007-03-05 17:08:4810楼 得分:0
love_2008(love2008)   的方法好。我的方法太傻了
发表于:2007-03-05 17:12:2511楼 得分:0
可否表达的简短一点?
发表于:2007-03-06 16:22:0512楼 得分:0
select   min(a),   b,   c   from   test   where   a   <>   b   group   by   b,c;
发表于:2007-03-07 08:38:1513楼 得分:0
估計意思不是很難,但樓主文字表達不清
发表于:2007-03-07 13:15:2714楼 得分:0
没有oracle数据库   用sql   server   实现了一下,看看对楼主有没有启发:
1.建一个区分大小写的表

create   table   [test]   (
[col1]   [varchar]   (20)   collate   chinese_prc_cs_as   null   ,
[col2]   [varchar]   (20)   collate   chinese_prc_cs_as   null   ,
[col3]   [varchar]   (20)   collate   chinese_prc_cs_as   null  
)   on   [primary]
go

2.插入原始数据

insert   into   test(col1,col2,col3)
select   'a ', 'a ', '1 '   union   all
select   'a ', 'a ', '2 '   union   all
select   'b ', 'a ', '3 '   union   all
select   'b ', 'a ', '4 '   union   all
select   'c ', 'a ', '5 '   union   all
select   'c ', 'a ', '6 '   union   all
select   'a ', 'b ', '7 '   union   all
select   'a ', 'b ', '8 '   union   all
select   'a ', 'a ', '9 '   union   all
select   'a ', 'a ', '10 '   union   all
select   'b ', 'b ', '11 '   union   all
select   'd ', 'c ', '12 '   union   all
select   'd ', 'c ', '13 '   union   all
select   'e ', 'c ', '14 '   union   all
select   'e ', 'c ', '15 '

3.选择所需数据

select   test.col1,test.col2,test.col3   from   test   inner   join

select   min(col1)   as   col1,   col2   from   test   where   col1 <> col2   group   by   col2
)t1
on   test.col1 <> test.col2
  and   test.col1=t1.col1
  and   test.col2=t1.col2

4.结果

col1                                   col2                                   col3                                  
--------------------   --------------------   --------------------  
a                                         a                                         1
a                                         a                                         2
a                                         b                                         7
a                                         b                                         8
d                                         c                                         12
d                                         c                                         13

(所影响的行数为   6   行)


完!
发表于:2007-03-09 16:58:5915楼 得分:0
select   *   from   (select   rank()   over(partition   by   col2   order   by   col3   desc)   rk,a.*   from   a)   t
where   t.rk <=2;
发表于:2007-03-09 17:34:5216楼 得分:0
select   t.col1,t.col2,t.col3   from   (select   *   from   (select   rank()   over(partition   by   col2   order   by     col3   )   rk,col1,col2,col3   from   test)   t   where   t.rk <=2   )t   order   by     t.col3

如果col3是number型的就没有问题,要是varchar2的话还需要再排一下序
发表于:2007-03-12 15:55:4017楼 得分:0
该回复于2007-12-28 18:23:37被管理员或版主删除


快速检索

最新资讯
热门点击