如何在没有子查询的情况下找到最大值
How to find the max value without subquery
为了获得下面的结果集,我写了以下 SQL:
SELECT t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film as t1
INNER JOIN country as t2 on t1.FilmCountryId = t2.CountryID
WHERE t1.FilmRunTimeMinutes = ( SELECT max(t2.FilmRunTimeMinutes)
FROM film as t2
WHERE t2.FilmCountryId = t1.FilmCountryId
)
ORDER BY FilmRunTimeMinutes DESC
我读了这个 Link 并尝试了同样的方法,但我做不到。那么如何使用 LEFT OUTER JOIN
获得相同的结果集呢?
电影 table 有那些列:
FilmId --PK
FilmName
FilmCountryId --FK
FilmRunTimeMinutes
国家/地区 table 有那些列:
CountryId --PK
CountryName
提前致谢。
使用Row_Number
window函数
SELECT TOP 1 WITH ties t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film AS t1
INNER JOIN country AS t2
ON t1.FilmCountryId = t2.CountryID
ORDER BY Row_number() OVER(partition BY FilmCountryId ORDER BY FilmRunTimeMinutes DESC),
FilmRunTimeMinutes DESC;
或使用CTE/Sub-Select
WITH cte
AS (SELECT t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes,
Rn = Row_number() OVER(partition BY FilmCountryId ORDER BY FilmRunTimeMinutes DESC)
FROM Film AS t1
INNER JOIN country AS t2
ON t1.FilmCountryId = t2.CountryID)
SELECT *
FROM cte
WHERE Rn = 1
ORDER BY FilmRunTimeMinutes DESC
如果你真的想要 left join
方法那么
SELECT t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film AS t1
INNER JOIN country AS t2
ON t1.FilmCountryId = t2.CountryID
LEFT JOIN Film AS t3
ON t3.FilmCountryId = t2.CountryID
AND t3.FilmRunTimeMinutes > t1.FilmRunTimeMinutes
WHERE t3.FilmID IS NULL
ORDER BY FilmRunTimeMinutes DESC
试试这个
;WITH Q
AS
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY t1.FilmCountryId ORDER BY t2.FilmRunTimeMinutes DESC),
t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film as t1
INNER JOIN country as t2 on t1.FilmCountryId=t2.CountryID
ORDER BY FilmRunTimeMinutes DESC
)
SELECT
*
FROM Q
WHERE RN = 1
为了获得下面的结果集,我写了以下 SQL:
SELECT t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film as t1
INNER JOIN country as t2 on t1.FilmCountryId = t2.CountryID
WHERE t1.FilmRunTimeMinutes = ( SELECT max(t2.FilmRunTimeMinutes)
FROM film as t2
WHERE t2.FilmCountryId = t1.FilmCountryId
)
ORDER BY FilmRunTimeMinutes DESC
我读了这个 Link 并尝试了同样的方法,但我做不到。那么如何使用 LEFT OUTER JOIN
获得相同的结果集呢?
电影 table 有那些列:
FilmId --PK FilmName FilmCountryId --FK FilmRunTimeMinutes
国家/地区 table 有那些列:
CountryId --PK CountryName
提前致谢。
使用Row_Number
window函数
SELECT TOP 1 WITH ties t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film AS t1
INNER JOIN country AS t2
ON t1.FilmCountryId = t2.CountryID
ORDER BY Row_number() OVER(partition BY FilmCountryId ORDER BY FilmRunTimeMinutes DESC),
FilmRunTimeMinutes DESC;
或使用CTE/Sub-Select
WITH cte
AS (SELECT t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes,
Rn = Row_number() OVER(partition BY FilmCountryId ORDER BY FilmRunTimeMinutes DESC)
FROM Film AS t1
INNER JOIN country AS t2
ON t1.FilmCountryId = t2.CountryID)
SELECT *
FROM cte
WHERE Rn = 1
ORDER BY FilmRunTimeMinutes DESC
如果你真的想要 left join
方法那么
SELECT t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film AS t1
INNER JOIN country AS t2
ON t1.FilmCountryId = t2.CountryID
LEFT JOIN Film AS t3
ON t3.FilmCountryId = t2.CountryID
AND t3.FilmRunTimeMinutes > t1.FilmRunTimeMinutes
WHERE t3.FilmID IS NULL
ORDER BY FilmRunTimeMinutes DESC
试试这个
;WITH Q
AS
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY t1.FilmCountryId ORDER BY t2.FilmRunTimeMinutes DESC),
t1.FilmName,
t2.CountryName,
t1.FilmRunTimeMinutes
FROM Film as t1
INNER JOIN country as t2 on t1.FilmCountryId=t2.CountryID
ORDER BY FilmRunTimeMinutes DESC
)
SELECT
*
FROM Q
WHERE RN = 1