Oracle 随机段 select
Oracle random segments select
我想select下面的片段。
Random 5500 rows including the following segments:
Subcategorie (sex): - 3300 men
- 2200 women
Subcategorie (age): - 2140 between 18-34 years
- 2100 between 35-54 years
- 1260 between 55-99 years
如何在 select 语句中解决这个问题?
问题是,您使用 "random" 这个词,但您按年龄和性别对群组进行了非常精确的细分。真正随机的单个查询不会产生如此精确的配额。所以你的查询一定很复杂:你需要将整个 table 分成满足你的约束的子集,然后从这些子集中随机 select 。像这样...
select * from (
select * from whatever
where sex = 'M'
and age between 18 and 34
order by dbms_random.value
)
where rownum <= 1284
union all
select * from (
select * from whatever
where sex = 'M'
and age between 35 and 54
order by dbms_random.value
)
where rownum <= 1260
union all select * from (
select * from whatever
where sex = 'M'
and age between 55 and 99
order by dbms_random.value
)
where rownum <= 756
union all
select * from (
select * from whatever
where sex = 'F'
and age between 18 and 34
order by dbms_random.value
)
where rownum <= 856
union all
select * from (
select * from whatever
where sex = 'F'
and age between 35 and 54
order by dbms_random.value
)
where rownum <= 840
union all select * from (
select * from whatever
where sex = 'F'
and age between 55 and 99
order by dbms_random.value
)
where rownum <= 504
这可能会表现不佳,具体取决于常见因素 - table 的大小、索引等 - 但它会产生那些确切的同类群组。
如果不是很明显,rownum
界限是每个年龄组的命中数乘以男女比例 (3:2)。
我想select下面的片段。
Random 5500 rows including the following segments:
Subcategorie (sex): - 3300 men
- 2200 women
Subcategorie (age): - 2140 between 18-34 years
- 2100 between 35-54 years
- 1260 between 55-99 years
如何在 select 语句中解决这个问题?
问题是,您使用 "random" 这个词,但您按年龄和性别对群组进行了非常精确的细分。真正随机的单个查询不会产生如此精确的配额。所以你的查询一定很复杂:你需要将整个 table 分成满足你的约束的子集,然后从这些子集中随机 select 。像这样...
select * from (
select * from whatever
where sex = 'M'
and age between 18 and 34
order by dbms_random.value
)
where rownum <= 1284
union all
select * from (
select * from whatever
where sex = 'M'
and age between 35 and 54
order by dbms_random.value
)
where rownum <= 1260
union all select * from (
select * from whatever
where sex = 'M'
and age between 55 and 99
order by dbms_random.value
)
where rownum <= 756
union all
select * from (
select * from whatever
where sex = 'F'
and age between 18 and 34
order by dbms_random.value
)
where rownum <= 856
union all
select * from (
select * from whatever
where sex = 'F'
and age between 35 and 54
order by dbms_random.value
)
where rownum <= 840
union all select * from (
select * from whatever
where sex = 'F'
and age between 55 and 99
order by dbms_random.value
)
where rownum <= 504
这可能会表现不佳,具体取决于常见因素 - table 的大小、索引等 - 但它会产生那些确切的同类群组。
如果不是很明显,rownum
界限是每个年龄组的命中数乘以男女比例 (3:2)。