0
votes

In SQL , How to get min / max date in the count , group by query?

Query :

-- This doesn't give Min and Max date received for the file

;with cte as
(
select SourceId,Count(FileName) as TotalFileCount
from mytable
group by SourceId
)
select * from cte
order by TotalFileCount desc

Expected output .. is along with the SourceId, Count, StartDate(Minimum), EndDate(Maximum)

enter image description here

1

1 Answers

2
votes

add the aggregation column you need

select SourceId
  , Count(FileName) as TotalFileCount
  , min(RecevidDate) InitialReceiveDate
  , max(RecevidDate) LastReceiveDate
from mytable
group by SourceId