我正在使用Chinook database来练习一些SQL,并且我正在尝试查找每个国家/地区购买的最受欢迎的相册(其中国家/地区由发票表中的BillingCountry确定)。我的解决方案(在MySQL中)如下:
SELECT TitleCount, Title, BillingCountry
FROM (SELECT max(TitleCount) AS TitleCount, BillingCountry
FROM (SELECT count(Title) AS TitleCount, BillingCountry
FROM Invoice NATURAL JOIN InvoiceLine
NATURAL JOIN Track NATURAL JOIN Album
GROUP BY Title, BillingCountry) AS counts
GROUP BY BillingCountry) AS maxCounts NATURAL JOIN
(SELECT count(Title) AS TitleCount, Title, BillingCountry
FROM Invoice NATURAL JOIN InvoiceLine
NATURAL JOIN Track NATURAL JOIN Album
GROUP BY Title, BillingCountry) AS counts;
别名子查询maxCounts为每个国家/地区查找最多的购买次数,并为每个国家/地区查找每个购买相册的次数。外部查询然后将两者与自然联接结合起来,有效地说,对于每个国家/地区,显示最多购买的相册。
简要概述下面的发票和专辑的表格(仅显示相关属性),这是与此问题最相关的两个。发票:
InvoiceId(PK) | BillingCountry
--------------+---------------
1 | USA
2 | Norway
3 | USA
4 | France
5 | USA
6 | France
专辑:
AlbumId(PK) | Title
------------+------
1 | Facelift
2 | Big Ones
3 | Out of Exile
发票和相册之间有许多与之相关的表格。我希望能够列出每个国家哪个专辑的购买量最大。因此,如果以下是某个国家/地区购买每张专辑的次数(实际上是上面的counts
子查询):
count(Title) | Title | Country
-------------+--------------+--------
3 | Facelift | USA
1 | Out of Exile | USA
2 | Big Ones | France
1 | Out of Exile | France
1 | Out of Exile | Norway
我希望在结果中看到的只是每个国家/地区最受欢迎的相册(由Title
代表):
max(Title) | Title | Country
-----------+--------------+--------
3 | Facelift | USA
2 | Big Ones | France
1 | Out of Exile | Norway
有没有更简洁的方法来写这个?我的第一个倾向是使用count
和GROUP BY
和HAVING
来查找具有最大数量的歌曲,如下所示:
SELECT count(Title) AS TitleCount, Title, BillingCountry
FROM Invoice NATURAL JOIN InvoiceLine NATURAL JOIN Track NATURAL JOIN Album
GROUP BY Title, BillingCountry
HAVING count(Title) = <finds max count>
但似乎HAVING
子查询必须知道当前国家/地区是为了找到该国家/地区的最大值。