[英]SQL get the min and max value of a column in a dataset with a single row
I'm using SQL Server and I have no idea how to execute this problem with a simple query.我正在使用 SQL Server,但我不知道如何使用简单的查询来执行此问题。 Assuming this table is named Records
假设这个表被命名为Records
RecordId | CreatedDate | ModifiedDate
1 | 11/07/2020 | 9/08/2020
1 | 03/06/2020 | 12/07/2020
2 | 06/05/2020 | 11/06/2020
3 | 12/12/2019 | 23/04/2020
3 | 05/01/2020 | 11/04/2020
And what I want is get the oldest CreatedDate and latest ModifiedDate per record id.我想要的是获取每个记录 ID 的最旧的 CreatedDate 和最新的 ModifiedDate。 My result set should be something like this.
我的结果集应该是这样的。
RecordId | CreatedDate | ModifiedDate
1 | 03/06/2020 | 9/08/2020
2 | 06/05/2020 | 11/06/2020
3 | 05/01/2020 | 23/04/2020
Thank you.谢谢你。
SELECT R.RECORDID,MIN(R.CREATEDDATE),MAX(R.MODIFIEDDATE)
FROM RECORDS AS R
GROUP BY R.RECORDID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.