在mssql下先排序再分组SQL语句

in #mssql6 years ago (edited)

在mysql下,我会使用select ... from (select ... from... ... order by ) tmp group by ...

无奈呀,在mssql下,子句中不支持order by

最后,在偶像 @justyy的帮助下,学会了OVER(PARTITION BY)的用法

partition by 可以按字段或字段组合进行分区,然后还可以在每个分区中按字段或字段组合进行排序

OVER(PARTITION BY)则可以从每个分区中查询数据

select author, permlink, 
min(DATEADD(hour,8, created)) over() (partition author,DATEADD(hour,8, created)) as time, 
from comments 
where DATEADD(hour,8, created)>='2018-01-27 00:00:00'
and DATEADD(hour,8, created)<='2018-02-03 00:00:00'
and permlink not like 're%'

伪程序员学习日记——边用边学边记——为了以后便于查询,也为了加强记忆。

欢迎各位偶像前来指正,不胜荣幸&感激!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 64573.45
ETH 3441.06
USDT 1.00
SBD 2.51