我是编程和学习SQL的新手。我正在尝试在ORDER BY ... DESC之后排除NULL值。您能帮忙解决这个问题吗?还不确定我应该使用WHERE还是HAVING,
-- Which director has the highest total domestic takings
SELECT d.first_name, d.last_name, SUM(mr.domestic_takings) AS total_dom_takings
FROM directors d
-- *** WHERE/HAVING mr.domestic_takings IS NOT NULL ***
JOIN movies mo ON d.director_id = mo.director_id
JOIN movie_revenues mr ON mo.movie_id = mr.movie_id
GROUP BY d.first_name, d.last_name
ORDER BY total_dom_takings DESC;
答案 0 :(得分:0)
尝试在JOIN之后编写WHERE子句:
SELECT d.first_name, d.last_name, SUM(mr.domestic_takings) AS total_dom_takings
FROM directors d
JOIN movies mo ON d.director_id = mo.director_id
JOIN movie_revenues mr ON mo.movie_id = mr.movie_id
WHERE mr.domestic_takings IS NOT NULL
GROUP BY d.first_name, d.last_name
ORDER BY total_dom_takings DESC;
答案 1 :(得分:0)
您可以完全省略WHERE
子句。
大多数聚合函数(其中sum
始终忽略NULL参数。