说我有一张桌子:
CREATE TABLE Test_Scores
( last_name VARCHAR2(40),
score number(10));
我想在表格中选择最高和最低分数。这两个查询将是:
select last_name,score, 'MAX' as Score_Type from Test_Scores
where score = (select max(score) from Test_Scores)
-> Last_name | Score | Score_Type
Smith | 15 | Max
select last_name,score, 'MIN' as Score_Type from Test_Scores
where score = (select min(score) from Test_Scores)
-> Last_name | Score | Score_Type
Jones | 5 | Min
如何将这两行设置在一起?在SAS中我会使用set语句,在R中我会使用rbind。是否有pl sql等价物(Oracle 11g)?
最终输出将是:
Last_name | Score | Score_Type
Smith | 15 | Max
Jones | 5 | Min
提前致谢。
答案 0 :(得分:1)
这可以使用普通SQL和窗口(又名"解析")功能来完成。此处不需要PL / SQL(PL / SQL仅 用于存储过程)
select last_name,
score
from (
select last_name,
score,
dense_rank() over (order by score desc) as ranked_first,
dense_rank() over (order by score asc) as ranked_last
from test_scores
)
where ranked_first = 1
or ranked_last = 1;