罪行表包含有关所犯罪行的信息。我试图获得哪个犯罪季度(1,2,3,4)犯罪数量最多。
SELECT financialquarter_ID, SUM(numberOfOffences) FROM offences WHERE financialquarter_ID = 1;
此查询输出4160
SELECT financialquarter_ID, SUM(numberOfOffences) FROM offences WHERE financialquarter_ID = 2;
此查询输出4227
SELECT financialquarter_ID, SUM(numberOfOffences) FROM offences WHERE financialquarter_ID = 3;
此查询输出4647
SELECT financialquarter_ID, SUM(numberOfOffences) FROM offences WHERE financialquarter_ID = 4;
此查询输出4665
因此,我们可以看到四分之一犯罪率最高。我正在尝试查询以输出此financialquarter_ID的ID(第4章)。
到目前为止,我尝试了以下查询,但均未成功。
select yt.financialquarter_ID, yt.numberOfOffences
From offences yt
where numberOfOffences =
(select max(numberOfOffences) from offences st where yt.financialquarter_ID = st.financialquarter_ID)
GROUP by numberOfOffences DESC
LIMIT 1
答案 0 :(得分:1)
使用group by
和limit
:
SELECT financialquarter_ID, SUM(numberOfOffences)
FROM offences
GROUP BY financialquarter_ID
ORDER BY SUM(numberOfOffences) DESC
LIMIT 1;
注意:如果发生平局,则仅返回一行。
答案 1 :(得分:0)
这将起作用:
SELECT financialquarter_ID, SUM(numberOfOffences) FROM offences group by
financialquarter_ID
having SUM(numberOfOffences)=(SELECT count(*) FROM offences group by
financialquarter_ID order by
count(*) desc limit 1
);