Oracle加入2个选择每个查询只有一条记录的语句

时间:2015-11-26 18:01:02

标签: sql oracle

目前我有2个需要运行的查询。我试图检索每个查询的第一条记录并将它们组合在一起。

问题摘要:获得类别,高级和超过60的最佳得分手,并显示结果。

以下是我正在运行的查询:

SELECT * FROM
(SELECT C2.*, RE.POINTS
FROM COMPETITION C1, COMPETITOR C2, RESULTS RE, REGISTRATION REG
WHERE REG.CATEGORYTYPE IN ('SENIOR')
AND C1.TIMEPLANNED BETWEEN TO_DATE('01-JAN-15') AND TO_DATE ('31-DEC-15')
AND REG.COMPETITIONID = RE.COMPETITONID
AND RE.COMPETITORID = C2.COMPETITORID
AND RE.COMPETITONID = C1.COMPETITIONID
ORDER BY RE.POINTS DESC)
WHERE ROWNUM=1;

第二个查询与第一个查询非常相似,仅在categorytype

中进行了更改
WHERE REG.CATEGORYTYPE IN ('OVER60')

我可以知道如何一起加入这两个陈述?

谢谢。

2 个答案:

答案 0 :(得分:1)

切换到ROW_NUMBER

SELECT * FROM
 ( 
   SELECT C2.*, RE.POINTS,
      ROW_NUMBER()
      OVER (PARTITION BY REG.CATEGORYTYPE
            ORDER BY RE.POINTS DESC) AS rn
   FROM COMPETITION C1, COMPETITOR C2, RESULTS RE, REGISTRATION REG
   WHERE REG.CATEGORYTYPE IN ('SENIOR', 'OVER60')
     AND C1.TIMEPLANNED BETWEEN TO_DATE('01-JAN-15') AND TO_DATE ('31-DEC-15')
     AND REG.COMPETITIONID = RE.COMPETITONID
     AND RE.COMPETITORID = C2.COMPETITORID
     AND RE.COMPETITONID = C1.COMPETITIONID
 )
WHERE rn=1;

答案 1 :(得分:0)

试试这个: 在你的where子句REG.CATEGORYTYPE中添加它('senior','over60')