好好学习,天天向上! avatar

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

dailystudy

Published: 03 Feb 2018 › Updated: 03 Feb 2018在mssql下先排序再分组SQL语句

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

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

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

最后,在偶像 justyy@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%'

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

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

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

Written by

伪程序员学习日记! Learning diary of a quasi-programmer

Read more #mssql posts


Best Posts From 好好学习,天天向上!

We have not curated any of dailystudy's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From 好好学习,天天向上!