使用ROW_NUMBER和PARTITION BY获取第一行和最后一行

时间:2016-07-21 13:55:47

标签: sql sql-server tsql window-functions row-number

样本输入

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

期望输出

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

尝试查询

我正在尝试使用ROW_NUMBER()PARTITION BY来获取最新的NameValue,但我还希望获得最早和最新的Timestamp值:

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

6 个答案:

答案 0 :(得分:5)

这可以使用窗口函数minmax完成。

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

Example

编辑:根据评论

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

编辑:另一种方法是使用first_value窗口函数,这将消除对子查询和连接的需要。

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

答案 1 :(得分:1)

您可以使用MINMAX函数+ OUTER APPLY

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

输出:

{{1}}

答案 2 :(得分:1)

除了MIN(Timestamp) OVER (PARTITION BY Name)列之外,还可以使用ROW_NUMBER(),如下所示:

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

答案 3 :(得分:0)

如果我正确理解你的问题,这里有两个使用operator[]功能的选项。然后,要将它们放在同一行,您可以使用row_number

这应该是关闭的:

conditional aggregation

答案 4 :(得分:0)

想想简单。

select 
    t.Name, 
    MAX(t.Value), 
    MIN(t.Timestamp), 
    MAX(t.Timestamp) 
FROM 
    t
group by 
    t.Name

答案 5 :(得分:0)

如果我理解了您的问题,请使用row_number()功能,如下所示:

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