写2个SQL查询

时间:2015-05-31 14:23:48

标签: sql oracle11g

Ex_complex is an exercise complex

这是我的ERD。我使用Oracle SQL。我需要:

1)选择涉及最多肌肉的运动。

2)根据所涉及肌肉的数量选择前5名练习。

1 个答案:

答案 0 :(得分:0)

我写过的查询: 它是第一个任务,第二个任务是相同的,除了R< 6:

SELECT *
FROM exercise
WHERE exercise.ex_id = any
    (SELECT ex_id
     FROM
       (SELECT e.ex_id, dense_rank() over (ORDER BY count(*) DESC) AS R
        FROM muscle m, muscle_group mg, ex_complex ex_c, exercise e
        WHERE m.mgroup_id = mg.mgroup_id
          AND ex_c.mgroup_id = mg.mgroup_id
          AND ex_c.ex_id = e.ex_id
        GROUP BY e.ex_id)
     WHERE R < 2);