按最大日期和ID分组

时间:2017-05-19 07:13:23

标签: sql sql-server select time group-by

我有下表:

 id         value       date
 ---------  ---------  ----------
 1          324          2017-05-19 02:00:00
 2          564          2017-05-19 04:00:00
 3          675          2017-05-19 03:00:00
 3          642          2017-05-19 05:00:00
 4          642          2017-05-19 07:00:00
 5          642          2017-05-19 06:00:00
 6          642          2017-05-19 05:00:00
 6          642          2017-05-19 12:00:00
 7          642          2017-05-19 01:00:00
 8          642          2017-05-19 13:00:00
 8          642          2017-05-19 02:00:00

我想GROUP BYid,但是当按value降序排序时,也会从第一行中选择datedate列。 id

结果应该是这样的:

 id         value       date
 ---------  ---------  ----------
 1          324          2017-05-19 02:00:00
 2          564          2017-05-19 04:00:00
 3          642          2017-05-19 05:00:00
 4          642          2017-05-19 07:00:00
 5          642          2017-05-19 06:00:00
 6          642          2017-05-19 12:00:00
 7          642          2017-05-19 01:00:00
 8          642          2017-05-19 13:00:00

5 个答案:

答案 0 :(得分:4)

这样的事情应该有效:

SELECT t1.id, t1.value, t1.date
FROM your_table t1
INNER JOIN (
    SELECT id, MAX(date) date
    FROM your_table
    GROUP BY id
) t2
ON t1.id = t2.id AND t1.date = t2.date

答案 1 :(得分:1)

试试这个

select id, value, max(date) 
from table_name 
group by id,value; 

答案 2 :(得分:0)

试试这个

select id, value, date 
from  name_table
group by id, value, date 

答案 3 :(得分:0)

尝试此查询...

SELECT t1.id,value,t1.date FROM new_table t1   
JOIN 
(SELECT nt.id,max(nt.date) AS date  FROM new_table as nt group by nt.id ) AS t2 
ON t1.date = t2.date AND t1.id = t2.id;

它会起作用..

询问是否有任何疑问。

答案 4 :(得分:0)

;With cte( id, value ,date)
AS
(

 SELECT 1, 324,'2017-05-19 02:00:00' Union all
 SELECT 2, 564,'2017-05-19 04:00:00' Union all
 SELECT 3, 675,'2017-05-19 03:00:00' Union all
 SELECT 3, 642,'2017-05-19 05:00:00' Union all
 SELECT 4, 642,'2017-05-19 07:00:00' Union all
 SELECT 5, 642,'2017-05-19 06:00:00' Union all
 SELECT 6, 642,'2017-05-19 05:00:00' Union all
 SELECT 6, 642,'2017-05-19 12:00:00' Union all
 SELECT 7, 642,'2017-05-19 01:00:00' Union all
 SELECT 8, 642,'2017-05-19 13:00:00' Union all
 SELECT 8, 642,'2017-05-19 02:00:00' 
 )
SELECT id
    ,value
    ,DATE
FROM (
    SELECT *
        ,ROW_NUMBER() OVER (
            PARTITION BY id ORDER BY id
            ) RNO
    FROM (
        SELECT id
            ,value
            ,MAX([date]) OVER (
                PARTITION BY id ORDER BY id DESC
                ) AS [date]
            ,ROW_NUMBER() OVER (
                PARTITION BY DATE ORDER BY id
                ) seq
        FROM cte
        ) ddt
    ) Final
WHERE Final.RNO = 1
ORDER BY Final.RNO

输出

id  value   DATE
---------------------------
1   324     2017-05-19 02:00:00
2   564     2017-05-19 04:00:00
3   642     2017-05-19 05:00:00
4   642     2017-05-19 07:00:00
5   642     2017-05-19 06:00:00
6   642     2017-05-19 12:00:00
7   642     2017-05-19 01:00:00
8   642     2017-05-19 13:00:00