SELECT DISTINCT col1, col2 FROM table t ORDER BY col1;
这给了我col1
&的独特组合。 col2
。是否有另一种编写Oracle SQL查询的方法来获得col1
&的唯一组合。 col2
记录没有使用关键字distinct?
答案 0 :(得分:6)
使用UNIQUE关键字,它是DISTINCT的同义词:
SELECT UNIQUE col1, col2 FROM table t ORDER BY col1;
答案 1 :(得分:5)
我不明白为什么你会这样做,但你可以做到
SELECT col1, col2 FROM table_t GROUP BY col1, col2 ORDER BY col1
答案 2 :(得分:3)
select col1, col2
from table
group by col1, col2
order by col1
或不太优雅的方式:
select col1,col2 from table
UNION
select col1,col2 from table
order by col1;
或更不优雅的方式:
select a.col1, a.col2
from (select col1, col2 from table
UNION
select NULL, NULL) a
where a.col1 is not null
order by a.col1
答案 3 :(得分:3)
另一个 - 但过于复杂且有些无用 - 的解决方案:
select *
from (
select col1,
col2,
row_number() over (partition by col1, col2 order by col1, col2) as rn
from the_table
)
where rn = 1
order by col1
答案 4 :(得分:3)
又一个......
select
col1,
col2
from
table t1
where
not exists (select *
from table t2
where t2.col1 = t1.col1 and
t2.col2 = t1.col2 and
t2.rowid > t1.rowid)
order by
col1;
答案 5 :(得分:2)
@aF对UNION
解决方案的变化。 :
<强> INTERSECT
强>
SELECT col1, col2 FROM tableX
INTERSECT
SELECT col1, col2 FROM tableX
ORDER BY col1;
<强> MINUS
强>
SELECT col1, col2 FROM tableX
MINUS
SELECT col1, col2 FROM tableX WHERE 0 = 1
ORDER BY col1;
MINUS
(第二版,如果有(NULL, NULL)
组,则会返回比其他版本少一行的行
SELECT col1, col2 FROM tableX
MINUS
SELECT NULL, NULL FROM dual
ORDER BY col1;
答案 6 :(得分:1)
另一个......
select col1,
col2
from (
select col1,
col2,
rowid,
min(rowid) over (partition by col1, col2) min_rowid
from table)
where rowid = min_rowid
order by col1;