select 10 rows per day with order

前端 未结 9 565
感情败类
感情败类 2021-02-06 16:08

i have a db with records with date (timestamp) i need to select 10 records for each day (there are many more per day) and order them by few columns...

how should that qu

9条回答
  •  被撕碎了的回忆
    2021-02-06 16:34

    Actually it´s more like this, I had misplaced the order by for the date.

    Here we assume there is a datetime field in the Records table and a score field to rank records.

    SELECT      *
    FROM            (SELECT *, ROW_NUMBER() OVER (PARTITION BY datepart(year, Record.date), datepart(month, Record.date), datepart(day, Record.date)
    ORDER BY Record.score desc) AS row
    FROM            Record ) AS table1
    WHERE        row < 11
    ORDER BY datepart(year, Record.date) desc, datepart(month, Record.date) desc, datepart(day, Record.date) desc
    

提交回复
热议问题