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

in #mssql7 years ago (edited)

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

<p dir="auto">无奈呀,在mssql下,子句中不支持order by <p dir="auto"><span>最后,在偶像 <a href="/@justyy">@justyy的帮助下,学会了OVER(PARTITION BY)的用法 <p dir="auto">partition by 可以按字段或字段组合进行分区,然后还可以在每个分区中按字段或字段组合进行排序 <p dir="auto">OVER(PARTITION BY)则可以从每个分区中查询数据 <pre><code>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%' <hr /> <p dir="auto">伪程序员学习日记——边用边学边记——为了以后便于查询,也为了加强记忆。 <p dir="auto">欢迎各位偶像前来指正,不胜荣幸&感激!