Min,Max for a time Range and Sum of Records for the last date/time

Advertisement
Hi All,
I have a table with the following structure/data:
create table  Events   (
                                    [EventID]       
int                   NOT NULL,
                                    [Title]            
nvarchar(200)  NOT NULL,
                                    [SourceName]  nvarchar(20)    NOT NULL,
                                    [Type]             
int                  NOT NULL,
                                    [eDate]           
datetime
insert into Events values(100, 'Event 1', 'S01', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S07', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S08', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S09', 3,'2014-01-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S010', 3,'2014-01-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S03', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S04', 3,'2014-01-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S05', 3,'2014-01-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S06', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S01', 3,'2014-02-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S02', 3,'2014-02-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S03', 3,'2014-02-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S04', 3,'2014-02-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S05', 3,'2014-02-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S06', 3,'2014-02-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S01', 3,'2014-03-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S02', 3,'2014-03-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S03', 3,'2014-03-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S04', 3,'2014-03-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S05', 3,'2014-03-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S06', 3,'2014-03-01 00:00:00.000')
And I wrote the following query:
 select EventID as [Event ID],
       Title, 
       count(distinct(SourceName)) as [Instances], 
       Type,
       min(eDate) as  [First Detected],
       max(eDate) as [Last Detected],
       datediff(d,min(eDate),max(eDate)) as [Delta (days)]
from  Events
where type = 3
group by EventID, Title, Type
having max(eDate) <> min(eDate)
   and max(eDate) =(select top 1 eDate from Events order by eDate desc)
and I get the following results (see the instance number)
Event ID Title         Instances Type First Detected                      Last Detected                    
   Delta (days)
=============================================================================================================================
100         Event 1         5         3    2014-01-01 00:00:00.000     2014-03-01 00:00:00.000    
59
101         Event 2        
3         3    2014-01-01 00:00:00.000     2014-03-01 00:00:00.000     59
102         Event 3        
2         3    2014-01-01 00:00:00.000     2014-03-01 00:00:00.000     59
This is normal for this query however what I need to do is a little different.
In other words, while I need to show when we recorded a specific event first and last time,
I need to display the results for the last date/time when it was recorded. 
For example what I need to provide should look like this:
Event ID  Title                Instances        Type       First Detected                    
Last Detected                          Delta (days)
=============================================================================================================================
100         Event 1            2                   3           
2014-01-01 00:00:00.000     2014-03-01 00:00:00.000      59
101         Event 2            2                   3           
2014-01-01 00:00:00.000     2014-03-01 00:00:00.000      59
102         Event 3           
2                   3            2014-01-01 00:00:00.000     2014-03-01 00:00:00.000     
59
Could you please help me to fix this query?
TIA,
John
Advertisement

Replay

;With cte As
(Select EventID as [Event ID],
Title,
SourceName,
Type,
min(eDate) Over(Partition By EventID, Title, Type) as [First Detected],
max(eDate) Over(Partition By EventID, Title, Type) as [Last Detected],
eDate,
datediff(d,min(eDate) Over(Partition By EventID, Title, Type),max(eDate) Over(Partition By EventID, Title, Type)) as [Delta (days)],
max(eDate) Over() As MaxEDate
from Events
where type = 3)
Select [Event ID],
Title,
COUNT(Distinct SourceName) As Instances,
Type,
[First Detected],
[Last Detected],
[Delta (days)]
From cte
Where eDate = MaxEDate And [First Detected] <> [Last Detected]
Group By [Event ID],
Title,
Type,
[First Detected],
[Last Detected],
[Delta (days)];
Tom
P.S. Thanks for providing the DDL and data.  That is always very helpful.