0
我試圖找到頂部第一次聽到歌曲中最受用戶首由用戶喜歡在一年的某月在某一個月和一年
我使用了下面的數據
user-id date song
....... ....... ......
1 2017-9-30 song1
1 2017-9-10 song1
1 2017-3-12 song2
1 2017-9-01 song1
1 2017-12-31 song1
1 2017-09-12 song3
1 2017-10-11 song1
1 2017-09-09 song5
1 2017-10-08 song4
2 2017-07-12 song1
2 2017-12-31 song3
2 2017-10-12 song5
3 2017-10-11 song1
3 2017-10-09 song7
3 2017-10-08 song2
3 2017-07-12 song2
我試圖讓下面的輸出
user-id year month song count
....... ...... ...... ..... ......
1 2017 9 song1 3
我曾嘗試下面的查詢,但我在降序排列獲得的所有數據,我無法挑選最上面的數據
SELECT YEAR(date) AS year, MONTH(date) AS month,song,count(song) c
FROM table where id=1 and year(dt)=2017 and month(dt)=9
GROUP BY YEAR(date), MONTH(date) ,song ORDER BY c DESC
我甚至用
SELECT d.*
FROM (SELECT id,year(date) y,month(date) m,count(song) as c,
ROW_NUMBER() OVER (PARTITION BY month(date) ORDER BY COUNT(song) DESC) as seqnum
FROM table where id=1 and month(date)=9
GROUP BY month(date),year(date) song
) d
WHERE seqnum = 1
ORDER BY c, dt DESC ;
嘗試和它引發的錯誤作爲
錯誤在編譯時聲明:失敗:ParseException的線路5:37外來輸入 '歌' 期待)附近「 '
第二個查詢在子查詢組中的年份(日期)後缺少逗號。 (只是解決錯誤,而不是結果。 – xQbert