[英]SQL Server : getting multiple min/max values from one query
I have data similar to the following in SQL Server. 我在SQL Server中具有与以下类似的数据。 I would like to query the single table for the category, min value from
StartDate
and max value from EndDate
. 我想查询单个表的类别,
StartDate
最小值和EndDate
最大值。 Hopefully the following mock up of the data will help explain - thanks 希望下面的数据模拟将有助于解释-谢谢
DATA: 数据:
Category StartDate EndDate
-------------------------------
A 1/1/2018 1/11/2018
A 1/3/2018 1/13/2018
B 1/1/2018 1/11/2018
B 1/9/2018 1/19/2018
A 1/5/2018 1/15/2018
C 1/4/2018 1/14/2018
A 1/1/2018 1/11/2018
C 1/7/2018 1/17/2018
Desired result of query: 所需查询结果:
Category StartDate EndDate
--------------------------------
A 1/1/2018 1/15/2018
B 1/1/2018 1/19/2018
C 1/4/2018 1/17/2018
What's wrong with GROUP BY
and MIN()
& MAX()
? GROUP BY
和MIN()
& MAX()
什么问题?
select Category, min(startdate) startdate, max(enddate) enddate
from table t
group by Category;
Use group by 使用分组依据
select category, min(startDate), max(endDate)
from my_table
group by category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.