将两个查询连接在一起,其中一个查询具有子查询

时间:2015-07-18 19:03:33

标签: sql join oracle11g

您好我想一起加入这两个查询。我已经尝试了许多编写和重写代码的组合,但我已经陷入困境。

我想加入的两个查询是:

SELECT a.lastname, a.firstname
FROM athletes a,
(SELECT athlete#, COUNT(event#) 
    FROM results
    GROUP BY athlete#
    HAVING COUNT(event#) > 1 ) b 
WHERE a.athlete# = b.athlete#
ORDER BY a.lastname;

SELECT sc.datetime, e.name
FROM event e JOIN schedule sc USING (event#);

第一个查询会对有多个事件的运动员进行计数并返回正确数量19,当我尝试将第二个查询添加到第一个查询时,我最终会从结果表中提取大部分数据。< / p>

2 个答案:

答案 0 :(得分:0)

如果你想让运动员的比赛不止一次比赛,这似乎是一个合理的解释,那么:

select a.lastname, a.firstname, e.name, sc.datetime
from athletes a join
     (select r.*, count(*) over (partition by athelete#) as cnt
      from results r
     ) r
     on a.athlete# = r.athelete# and cnt > 1 join
     event e
     on r.event# = e.event# join
     schedule s
     on e.event# = s.event#;

答案 1 :(得分:0)

SELECT a.lastname, a.firstname, sc.datetime, e.name
FROM athletes a
inner join (SELECT athlete#, COUNT(event#) 
    FROM results
    GROUP BY athlete#
    HAVING COUNT(event#) > 1 ) b  on a.athlete# = b.athlete#
inner join results r on r.athlete# =b.athlete#
inner join event e on e.event# = r.event# 
inner join schedule sc on sc.event# = e.event#
ORDER BY a.lastname;