我有一个查询,在那里我想为每个时区找到前50 event_dates。虽然我正在了解这一点,但它目前还没有以可用的方式格式化。我希望结果看起来像:
ID : event_date \x{e 010}2017-08环2017年-08-08年中6区2017年-08-09中环7-08-09 Central 7环2017-08-07东部8-08 2017-08-08东部9连2017年-08-09东面10 2017- 08 2017年-08-07西11 2017-2017-07西11 2017-201708-08西部12 - 2017-08-09西部
目前,结果在这样的时区之间混在一起:
ID : event_date \x{e 010} 2017-08-08 -08-08-08-08-08-08山区2区2017-08-07市中心2区2017-08-07 Central 3区2017- 08东部10 2017-08-09 Central 10 2017-08 Central 11 2017-08 Central 11 2017-08 Central 11 201708-09东面12 - 2017-08-09西
我需要做什么才能以这种方式将查询更新为输出?
作为参考,我目前的情况如下:
SELECT TOP 200 ID, event_date, timezone FROM ...
ORDER BY ROW_NUMBER() OVER(PARTITION BY timezone ORDER BY timezone), timezone, event_date ASC发布于 2017-08-14 15:30:02
我将首先将ROW_NUMBER逻辑封装在子查询中,然后按每个时区前50行进行筛选。这也使查询不受添加到FROM表中的新时区的影响。
SELECT *
FROM (
SELECT ID
,event_date
,timezone
,ROW_NUMBER() OVER (
PARTITION BY timezone --these are the main groups you want
ORDER BY event_date DESC --DESC picks the LATEST dates first (ASC would pick the earliest)
) AS RowNumber
FROM...
) Results
WHERE RowNumber <= 50 --top X rows per timezone
ORDER BY timezone, RowNumberhttps://dba.stackexchange.com/questions/183437
复制相似问题