简体   繁体   中英

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. Assuming this table is named 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM