15

Sample Input

Name | Value | Timestamp
-----|-------|-----------------
One  | 1     | 2016-01-01 02:00
Two  | 3     | 2016-01-01 03:00
One  | 2     | 2016-01-02 02:00
Two  | 4     | 2016-01-03 04:00

Desired Output

Name | Value | EarliestTimestamp | LatestTimestamp
-----|-------|-------------------|-----------------
One  | 2     | 2016-01-01 02:00  | 2016-01-02 02:00
Two  | 4     | 2016-01-01 03:00  | 2016-01-03 04:00

Attempted Query

I am trying to use ROW_NUMBER() and PARTITION BY to get the latest Name and Value but I would also like the earliest and latest Timestamp value:

SELECT
    t.Name,
    t.Value,
    t.????????? AS EarliestTimestamp,
    t.Timestamp AS LatestTimestamp
FROM 
    (SELECT
        ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) AS RowNumber,
        Name,
        Value
        Timestamp) t
WHERE t.RowNumber = 1
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

6 Answers6

8

This can be done using window functions min and max.

select distinct name, 
min(timestamp) over(partition by name), max(timestamp) over(partition by name)
from tablename

Example

Edit: Based on the comments

select t.name,t.value,t1.earliest,t1.latest
from t 
join (select distinct name, 
      min(tm) over(partition by name) earliest, max(tm) over(partition by name) latest
      from t) t1 on t1.name = t.name and t1.latest = t.tm

Edit: Another approach is using the first_value window function, which would eliminate the need for a sub-query and join.

select distinct
name, 
first_value(value) over(partition by name order by timestamp desc) as latest_value,
min(tm) over(partition by name) earliest,
-- or first_value can be used 
-- first_value(timestamp) over(partition by name order by timestamp)
max(tm) over(partition by name) latest
-- or first_value can be used
-- first_value(timestamp) over(partition by name order by timestamp desc)
from t
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
  • Note that this query will work only if `value` grow in time. If we change `value` from 1 to 10 in the first row of OPs example - this query will produce wrong results. – gofr1 Jul 21 '16 at 14:34
  • @gofr1 yes. what you probably want to use is `last_value(value) over (partition by name order by timestamp)` – Chitharanjan Das Jul 21 '16 at 14:37
1

You can use MIN and MAX functions + OUTER APPLY:

SELECT  t.Name, 
        p.[Value],
        MIN(t.[Timestamp]) as EarliestTimestamp ,
        MAX(t.[Timestamp]) as LatestTimestamp
FROM Table1 t
OUTER APPLY (SELECT TOP 1 * FROM Table1 WHERE t.Name = Name ORDER BY [Timestamp] DESC) p
GROUP BY t.Name, p.[Value]

Output:

Name    Value   EarliestTimestamp   LatestTimestamp
One     2       2016-01-01 02:00    2016-01-02 02:00
Two     4       2016-01-01 03:00    2016-01-03 04:00
gofr1
  • 15,741
  • 11
  • 42
  • 52
1

If I'm understanding your question correctly, here's one option using the row_number function twice. Then to get them on the same row, you can use conditional aggregation.

This should be close:

SELECT
    t.Name,
    t.Value,
    max(case when t.minrn = 1 then t.timestamp end) AS EarliestTimestamp,
    max(case when t.maxrn = 1 then t.timestamp end) AS LatestTimestamp
FROM 
    (SELECT
        ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP) as minrn,
        ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) as maxrn,
        Name,
        Value
        Timestamp
     FROM YourTable) t
WHERE t.minrn = 1 or t.maxrn = 1
GROUP BY t.Name, t.Value
sgeddes
  • 62,311
  • 6
  • 61
  • 83
1

Use MIN(Timestamp) OVER (PARTITION BY Name) in addition to the ROW_NUMBER() column, like so:

SELECT
    t.Name,
    t.Value,
    t.EarliestTimestamp AS EarliestTimestamp,
    t.Timestamp AS LatestTimestamp
FROM 
    (SELECT
        ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) AS RowNumber,
        MIN(Timestamp) OVER (PARTITION BY Name) AS EarliestTimestamp,
        ^^
        Name,
        Value
        Timestamp) t
WHERE t.RowNumber = 1
Chitharanjan Das
  • 1,283
  • 10
  • 15
1

If I understood your question, use the row_number() function as follows:

SELECT  
    t.Name,  
    t.Value,  
    min(t.Timestamp) Over (Partition by name) As EarliestTimestamp,  
    t.Timestamp AS LatestTimestamp  
FROM   
    (SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) AS     RowNumber,  
        Name,  
        Value,  
        Timestamp) t  
WHERE t.RowNumber = 1  
Group By t.Name, t.Value, t.TimeStamp
neer
  • 4,031
  • 6
  • 20
  • 34
Aldo López
  • 431
  • 1
  • 4
  • 9
0

Think simple.

select 
    t.Name, 
    MAX(t.Value), 
    MIN(t.Timestamp), 
    MAX(t.Timestamp) 
FROM 
    t
group by 
    t.Name
neer
  • 4,031
  • 6
  • 20
  • 34