我正在尝试创建一个将由非洲大陆分组的sql,然后在2000年按降序排列每个大洲的总销售额。这就是我所做的:
SELECT SUM(price) AS SaleSum, continent, country
FROM fact as F, address as A, daterecord as D
WHERE F.address = A.id and F.transactionDate=D.id and D.`year`=2000
GROUP BY continent, country
ORDER BY SaleSum DESC
并且结果是输出仅按总销售额的下降排序,并且非洲没有分组。我该如何处理这个问题?谢谢!
更新
以上似乎有点令人困惑。我期待的是按非洲大陆分组的国家总销售额的排序结果。这就是说,对于每个大陆,该大陆的县都需要根据总销售额进行分类。对不起造成的混乱。
答案 0 :(得分:1)
首先,您应该学会使用正确的join
语法。简单规则:永远不要在from
子句中使用逗号。
其次,我认为你只需要order by
中的两个键:
SELECT SUM(price) AS SaleSum, continent, country
FROM fact F JOIN
address A
ON F.address = A.id JOIN
daterecord D
ON F.transactionDate = D.id
WHERE D.`year`=2000
GROUP BY continent, country
ORDER BY continent, SaleSum DESC;
答案 1 :(得分:0)
可能你需要使用HAVING子句
SELECT SUM(price) AS SaleSum, continent, country, d.year as year
FROM fact as F, address as A, daterecord as D
WHERE F.address = A.id and F.transactionDate=D.id
GROUP BY continent, country, year
HAVING year = 2000
ORDER BY SaleSum DESC