[英]Find the most frequent data every year in SQL
表A:
Years Data
2000 A
2000 B
2000 C
2000 C
2000 D
2001 A
2001 B
2001 B
2002 B
2002 D
2002 D
我想輸出:
Years Data
2000 C
2001 B
2002 D
我的解決方案:
SELECT DISTINCT Years, Data
FROM
(
SELECT Years, Data, COUNT(*) AS _count
FROM TableA
GROUP BY Years, Data
) a1
ORDER BY Years, _count DESC
但這有一個問題:
如果指定了
SELECT DISTINCT
則ORDER BY
項目必須出現在選擇列表中。
如何更正我的SQL代碼?
假設您的數據庫支持row_number(),則可以這樣進行:
SELECT Years, Data
FROM
(
SELECT Years,
Data,
ROW_NUMBER() OVER(PARTITION BY Years ORDER BY count(*) DESC) rn
FROM TableA
GROUP BY Years, Data
) x
WHERE rn = 1
ORDER BY Years, Data
嘗試這個:
select t.Years, t.[Data]
from (
select *, count(*) cnt
from TableA
group by years, [Data]
) t
left join (
select Years, max(cnt) maxCnt
from (
select *, count(*) cnt
from TableA
group by years, [Data]
) t
group by Years
) tt on t.Years = tt.Years -- tt is a view that gives you max count of each year
where t.cnt = tt.maxCnt -- you need `years`, `[Data]` that its count is max count
order by years;
另一種方法是在支持它的DBMS中使用rank()
:
;with t as (
select *, count(*) cnt
from TableA
group by years, [Data]
), tt as (
select *, rank() over (partition by years order by cnt desc) rn
from t
)
select years, [Data]
from tt
where rn = 1
order by years;
如果使用的是oracle,則可以使用STATS_MODE
函數
select years, stats_mode(data)
from tablet
group by years;
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions154.htm
您的錯誤是"ORDER BY items must appear in the select list if SELECT DISTINCT is specified."
這意味着您已在ORDER BY
中放入了不在SELECT
。 在這種情況下, _count DESC不在SELECT語句中
SELECT DISTINCT Years, Data, _count DESC
FROM
(
SELECT Years, Data, COUNT(*) AS _count
FROM TableA
GROUP BY Years, Data
) a1
ORDER BY Years, _count DESC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.