SQL选择具有重复ID的行中的最大值(日期)

时间:2017-09-06 18:52:44

标签: sql join duplicates

我有一张如下表所示的表格。我正在尝试获取状态为0的每个用户的行,如果有重复的UserId,则获取该用户的max(CreatedDate)行

表A

+----+--------+-------------+--------+
| Id | UserId | CreatedDate | Status |
+----+--------+-------------+--------+
|  1 | a234   | 09/02/2017  |      0 |
|  2 | a234   | 09/03/2017  |      0 |
|  3 | a234   | 09/06/2017  |      1 |
|  4 | a143   | 08/25/2017  |      0 |
+----+--------+-------------+--------+

预期输出

+----+--------+-------------+--------+
| Id | UserId | CreatedDate | Status |
+----+--------+-------------+--------+
|  2 | a234   | 09/03/2017  |      0 |
|  4 | a143   | 08/25/2017  |      0 |
+----+--------+-------------+--------+

SQL查询

SELECT d.Id, d.UserId,d.CreatedDate,d.Status FROM 
(
    SELECT Id, Max(CreatedDate) as MaxDate  
    FROM TableA
    GROUP BY Id
    HAVING Count(*)>1
) r
INNER JOIN TableA d
ON d.Id=r.Id AND d.CreatedDate=r.MaxDate AND Status=0

但我觉得条件错了,应该像序列一样:

  

第一个条件状态= 0

     

第二个条件如果存在重复的UserId,请使用Max(CreatedDate),否则使用CreatedDate

4 个答案:

答案 0 :(得分:3)

试试这个: -

SELECT d.Id, d.UserId,d.CreatedDate,d.Status FROM 
(
    SELECT UserId, Max(CreatedDate) as MaxDate  
    FROM TableA
    WHERE Status = 0
    GROUP BY UserId
) r
INNER JOIN TableA d
ON d.UserId=r.UserId AND d.CreatedDate=r.MaxDate AND Status=0

您的GROUP BYJOIN需要在UserId上才能生效,我已将过滤器Status=0移动到派生的表/子查询。< / p>

答案 1 :(得分:1)

GET_PROCESS_STATE_AND_OOM_SCORE

我认为你得到的结果是你没有预料到的是你的计数(*)&gt; 1&#34;只会返回重复项。

答案 2 :(得分:0)

WHERE

中使用子查询有一个解决方案
SELECT d.Id, d.UserId, d.CreatedDate,d.Status 
FROM TableA d
WHERE d.Status  = 0 and d.CreatedDate = 
      (
            SELECT MAX(x.CreatedDate)
            FROM TableA x
            WHERE x.Status  = 0 AND x.UserId = d.UserId
      )

另一种解决方案可能是使用NOT EXISTS

SELECT d.Id, d.UserId, d.CreatedDate,d.Status 
FROM TableA d
WHERE d.Status  = 0 and NOT EXISTS
      (
            SELECT MAX(x.CreatedDate)
            FROM TableA x
            WHERE x.Status  = 0 AND x.UserId = d.UserId AND 
                  x.CreatedDate > d.CreatedDate
      )

ALL

SELECT d.Id, d.UserId, d.CreatedDate,d.Status 
FROM TableA d
WHERE d.Status  = 0 and d.CreatedDate >= 
      ALL(
            SELECT x.CreatedDate
            FROM TableA x
            WHERE x.Status  = 0 AND x.UserId = d.UserId
      )

答案 3 :(得分:0)

我相信您会发现以下SQL更高效,更优雅。

SELECT Id, UserId, CreatedDate, Status,
MAX (CreatedDate) OVER (PARTITION BY USerId) AS max_CreatedDate
FROM TableA
WHERE Status = 0;

让我知道它是否适合您!

泰德。