在SQL中,如何获取count中的最小/最大日期(按查询分组)?
查询:
-这不会给出文件的最小和最大日期
;with cte as
(
select SourceId,Count(FileName) as TotalFileCount
from mytable
group by SourceId
)
select * from cte
order by TotalFileCount desc
预期的输出..以及SourceId,Count,StartDate(Minimum),EndDate(Maximum)
答案 0 :(得分:2)
添加所需的聚合列
select SourceId
, Count(FileName) as TotalFileCount
, min(RecevidDate) InitialReceiveDate
, max(RecevidDate) LastReceiveDate
from mytable
group by SourceId