table A :
title type
BB 甲
CC 甲
DD 乙
BB 乙
-------------------------------
查询之后要的结果是:
title type
BB 甲
CC 甲
DD 乙
--------------------------------
title有相同值而type不管相不相同时只随机选取一条记录
34 个解决方案
#1
distinct
#2
distinct 好像不行吧
用row_number()看看
用row_number()看看
#3
兄弟 你自己先试试看先吧
#4
随机?
select title, max(type)
from table
group by title
select title, max(type)
from table
group by title
#5
是啊 随机噻~!
#6
嗯,楼上的max, min都可以。只是随机性差了一点。
#7
这哪里随机了啊???
#8
路过学习了
#9
你只是让人随机挑一条,max, min也只是取其中一条,没有违背条件。特例而已。
#10
这样不能算随机啊。。。。。。
#11
算了,给你一个随机的吧:
SQL> select title, type from a where rowid = (select max(rowid) from a b where b.title = a.title);
TITLE TYPE
-------------------------------- ----
CC 甲
DD 乙
BB 乙
SQL> select title, type from a where rowid = (select max(rowid) from a b where b.title = a.title);
TITLE TYPE
-------------------------------- ----
CC 甲
DD 乙
BB 乙
#12
用Oracle分析函数,代码如下:
select * from (
select title ,type, row_number() over(partition by title) r from table A
) where r = 1;
select * from (
select title ,type, row_number() over(partition by title) r from table A
) where r = 1;
#13
请写清楚一些 好吗
那个b是什么呀?
#14
有语法错误。。。
#15
我只是给你一个方法,是伪代码,具体代码根据你那得实际情况来写啊,比如表名,你难道就不动照搬我的?
#16
更正楼上的:
SQL> select * from (
2 select title ,type, row_number() over(partition by title order by title) r from A
3 ) where r = 1;
TITLE TYPE R
-------------------------------- ---- ----------
BB 甲 1
CC 甲 1
DD 乙 1
SQL> select * from (
2 select title ,type, row_number() over(partition by title order by title) r from A
3 ) where r = 1;
TITLE TYPE R
-------------------------------- ---- ----------
BB 甲 1
CC 甲 1
DD 乙 1
#17
b是表a的别名。晕。已经够清楚了。
#18
还是没有随机噻~!~!~!~!
#19
你懂不懂什么叫随机啊??????????
#20
参考我在11楼的答复。
#21
汗,一定要随机啊。没注意这个
#22
select * from (
select title ,type, row_number() over(partition by title order by dbms_random.random) r from table A
) where r=1;
自己试下用dbms_random.random行不行,代码可能需要改
#23
同意楼上的! 按不同的列随机排序,得到的结果全部都是随机的!
而再加上用分析函数取一行记录,这就保证了随机取一条记录了!
#24
title有相同值而type不管相不相同时只
随机选取一条记录
create table a(
title varchar2(20),
type varchar2(20));
insert into a(title,type) values('BB', '甲');
insert into a(title,type) values('CC', '甲');
insert into a(title,type) values('DD', '乙');
insert into a(title,type) values('BB', '乙');
commit;
-- 在row_number() over(partition by ... order by sys_guid()),
-- 这样:利用sys_guid()随机函数即可满足楼主需求
select a.title, a.type
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.rcn=1;
#25
-- 这样的问题太常见了,好像不只回答一百次了!
#26
跟我那个思路一样的,就是用的随机数产生方法不一样。你这个经过验证没,我那个没验证过由于环境的原因
#27
-- 你测试一下,不就知道了?
-- 动手就这么难么?
#28
select k.title,k.type from (
select e.title,e.type,row_number() over(partition by e.title order by e.rs) rss from (
select a.title,a.type,dbms_random.value rs from a ) e ) k
where k.rss=1
;
select e.title,e.type,row_number() over(partition by e.title order by e.rs) rss from (
select a.title,a.type,dbms_random.value rs from a ) e ) k
where k.rss=1
;
#29
要下班了,说说偶的思路吧。对待选的名字赋值以行号,然后对行号范围用random取随机数,这样就随机了。。。
#30
就这个了。
#31
select a.title, a.type
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.RCN =ROUND(DBMS_RANDOM.value(1,2));
应该这样才是真正的随机。
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.RCN =ROUND(DBMS_RANDOM.value(1,2));
应该这样才是真正的随机。
#32
sys_guid()一般也会处于增长状态,所以也不够随机
第一次执行结果:
1 BB 甲
2 CC 乙
3 DD 乙
第二次执行结果:
1 BB 甲
2 CC 丙
3 DD 乙
第三次执行结果:
1 BB 乙
2 CC 甲
3 DD 乙
with t as
(select 'BB' title, '甲' type
from dual
union
select 'CC' title, '甲' type
from dual
union
select 'CC' title, '乙' type
from dual
union
select 'CC' title, '丙' type
from dual
union
select 'DD' title, '乙' type
from dual
union
select 'BB' title, '乙' type from dual)
select t1.title, t1.type
from (select title,
type,
row_number() over(partition by title order by title) rn
from t) t1,
(select title, count(type) c from t group by title) t2
where t1.title = t2.title
and t1.rn = round(dbms_random.value(1, t2.c))
第一次执行结果:
1 BB 甲
2 CC 乙
3 DD 乙
第二次执行结果:
1 BB 甲
2 CC 丙
3 DD 乙
第三次执行结果:
1 BB 乙
2 CC 甲
3 DD 乙
#33
感觉好无聊啊
#34
with t as
(select 'BB' title, '甲' type
from dual
union
select 'CC' title, '甲' type
from dual
union
select 'CC' title, '乙' type
from dual
union
select 'CC' title, '丙' type
from dual
union
select 'DD' title, '乙' type
from dual
union
select 'BB' title, '乙' type from dual
)
select *
from (select title,
type,
row_number() over(partition by title order by title desc) rn
from t) t1
where rn = 1
#1
distinct
#2
distinct 好像不行吧
用row_number()看看
用row_number()看看
#3
兄弟 你自己先试试看先吧
#4
随机?
select title, max(type)
from table
group by title
select title, max(type)
from table
group by title
#5
是啊 随机噻~!
#6
嗯,楼上的max, min都可以。只是随机性差了一点。
#7
这哪里随机了啊???
#8
路过学习了
#9
你只是让人随机挑一条,max, min也只是取其中一条,没有违背条件。特例而已。
#10
这样不能算随机啊。。。。。。
#11
算了,给你一个随机的吧:
SQL> select title, type from a where rowid = (select max(rowid) from a b where b.title = a.title);
TITLE TYPE
-------------------------------- ----
CC 甲
DD 乙
BB 乙
SQL> select title, type from a where rowid = (select max(rowid) from a b where b.title = a.title);
TITLE TYPE
-------------------------------- ----
CC 甲
DD 乙
BB 乙
#12
用Oracle分析函数,代码如下:
select * from (
select title ,type, row_number() over(partition by title) r from table A
) where r = 1;
select * from (
select title ,type, row_number() over(partition by title) r from table A
) where r = 1;
#13
请写清楚一些 好吗
那个b是什么呀?
#14
有语法错误。。。
#15
我只是给你一个方法,是伪代码,具体代码根据你那得实际情况来写啊,比如表名,你难道就不动照搬我的?
#16
更正楼上的:
SQL> select * from (
2 select title ,type, row_number() over(partition by title order by title) r from A
3 ) where r = 1;
TITLE TYPE R
-------------------------------- ---- ----------
BB 甲 1
CC 甲 1
DD 乙 1
SQL> select * from (
2 select title ,type, row_number() over(partition by title order by title) r from A
3 ) where r = 1;
TITLE TYPE R
-------------------------------- ---- ----------
BB 甲 1
CC 甲 1
DD 乙 1
#17
b是表a的别名。晕。已经够清楚了。
#18
还是没有随机噻~!~!~!~!
#19
你懂不懂什么叫随机啊??????????
#20
参考我在11楼的答复。
#21
汗,一定要随机啊。没注意这个
#22
select * from (
select title ,type, row_number() over(partition by title order by dbms_random.random) r from table A
) where r=1;
自己试下用dbms_random.random行不行,代码可能需要改
#23
同意楼上的! 按不同的列随机排序,得到的结果全部都是随机的!
而再加上用分析函数取一行记录,这就保证了随机取一条记录了!
#24
title有相同值而type不管相不相同时只
随机选取一条记录
create table a(
title varchar2(20),
type varchar2(20));
insert into a(title,type) values('BB', '甲');
insert into a(title,type) values('CC', '甲');
insert into a(title,type) values('DD', '乙');
insert into a(title,type) values('BB', '乙');
commit;
-- 在row_number() over(partition by ... order by sys_guid()),
-- 这样:利用sys_guid()随机函数即可满足楼主需求
select a.title, a.type
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.rcn=1;
#25
-- 这样的问题太常见了,好像不只回答一百次了!
#26
跟我那个思路一样的,就是用的随机数产生方法不一样。你这个经过验证没,我那个没验证过由于环境的原因
#27
-- 你测试一下,不就知道了?
-- 动手就这么难么?
#28
select k.title,k.type from (
select e.title,e.type,row_number() over(partition by e.title order by e.rs) rss from (
select a.title,a.type,dbms_random.value rs from a ) e ) k
where k.rss=1
;
select e.title,e.type,row_number() over(partition by e.title order by e.rs) rss from (
select a.title,a.type,dbms_random.value rs from a ) e ) k
where k.rss=1
;
#29
要下班了,说说偶的思路吧。对待选的名字赋值以行号,然后对行号范围用random取随机数,这样就随机了。。。
#30
就这个了。
#31
select a.title, a.type
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.RCN =ROUND(DBMS_RANDOM.value(1,2));
应该这样才是真正的随机。
from (
select row_number() over(partition by a1.title order by sys_guid()) rcn, a1.title, a1.type
from a a1 ) a
where a.RCN =ROUND(DBMS_RANDOM.value(1,2));
应该这样才是真正的随机。
#32
sys_guid()一般也会处于增长状态,所以也不够随机
第一次执行结果:
1 BB 甲
2 CC 乙
3 DD 乙
第二次执行结果:
1 BB 甲
2 CC 丙
3 DD 乙
第三次执行结果:
1 BB 乙
2 CC 甲
3 DD 乙
with t as
(select 'BB' title, '甲' type
from dual
union
select 'CC' title, '甲' type
from dual
union
select 'CC' title, '乙' type
from dual
union
select 'CC' title, '丙' type
from dual
union
select 'DD' title, '乙' type
from dual
union
select 'BB' title, '乙' type from dual)
select t1.title, t1.type
from (select title,
type,
row_number() over(partition by title order by title) rn
from t) t1,
(select title, count(type) c from t group by title) t2
where t1.title = t2.title
and t1.rn = round(dbms_random.value(1, t2.c))
第一次执行结果:
1 BB 甲
2 CC 乙
3 DD 乙
第二次执行结果:
1 BB 甲
2 CC 丙
3 DD 乙
第三次执行结果:
1 BB 乙
2 CC 甲
3 DD 乙
#33
感觉好无聊啊
#34
with t as
(select 'BB' title, '甲' type
from dual
union
select 'CC' title, '甲' type
from dual
union
select 'CC' title, '乙' type
from dual
union
select 'CC' title, '丙' type
from dual
union
select 'DD' title, '乙' type
from dual
union
select 'BB' title, '乙' type from dual
)
select *
from (select title,
type,
row_number() over(partition by title order by title desc) rn
from t) t1
where rn = 1