Group by every N records in T-SQL

后端 未结 4 410
旧时难觅i
旧时难觅i 2021-01-03 23:26

I have some performance test results on the database, and what I want to do is to group every 1000 records (previously sorted in ascending order by date) an

4条回答
  •  悲哀的现实
    2021-01-03 23:34

    WITH T AS (
      SELECT RANK() OVER (ORDER BY ID) Rank,
        P.Field1, P.Field2, P.Value1, ...
      FROM P
    )
    SELECT (Rank - 1) / 1000 GroupID, AVG(...)
    FROM T
    GROUP BY ((Rank - 1) / 1000)
    ;
    

    Something like that should get you started. If you can provide your actual schema I can update as appropriate.

提交回复
热议问题