【问题标题】:Getting Distinct Id(FK) by the latest date in SQL Server在 SQL Server 中按最新日期获取 Distinct Id(FK)
【发布时间】:2019-08-03 16:09:53
【问题描述】:

我目前正在处理一个查询,我需要根据创建的最新日期来区分 ID。

这是我的图表

|  ID  |    ModelID     |    LocationId   |  DateCreated  |
|------+----------------+-----------------+---------------|
|   1  |       1        |        5        |   02/03/2019  |
|   2  |       2        |      null       |   01/14/2019  |
|   3  |       2        |        0        |   02/03/2019  |
|   4  |       2        |        5        |   12/30/2018  |
|   5  |       4        |        3        |   01/10/2019  |
|   6  |       3        |        5        |   02/14/2019  |
|   7  |       2        |        5        |   03/13/2019  |

所以我的目标是区分ModelId,但仍然显示LocationId is null or equals to 0,如果LocationId 仍然相同,获取最新的DateCreated 并将其显示到列表中

期望的输出:

    |  ID  |    ModelID     |    LocationId   |  DateCreated  |
    +------+----------------+-----------------+---------------+
    |   1  |       1        |        5        |   02/03/2019  |
    |   2  |       2        |      null       |   01/14/2019  |<-still show
    |   3  |       2        |        0        |   02/03/2019  |<-still show
    |   4  |       2        |        5        |   03/13/2019  |<-The Latest
    |   5  |       4        |        3        |   01/10/2019  |
    |   6  |       3        |        5        |   02/14/2019  |

这是我当前的 SQL 查询:

SELECT 
    a.ModelID, a.LocationId,
    (SELECT TOP 1 
         CONVERT(NVARCHAR, DateCreated, 101) 
     FROM 
         db.DeliveryDates 
     WHERE
         isDeleted = 0 
         AND DeliveryId = a.DeliveryId) AS DateCreated,
FROM 
    db.DeliveryCarInfo a
WHERE
    a.isDeleted = 0

【问题讨论】:

    标签: sql sql-server sql-server-2008 select distinct


    【解决方案1】:

    请尝试以下代码

    DECLARE @DeliveryCarInfo TABLE (id int,modelId int,locationId int null,DateCreated date)
    
    INSERT INTO @DeliveryCarInfo
    (
        id,
        modelId,
        locationId,
        DateCreated
    )
    VALUES
    (1  ,1 ,5,'02/03/2019'),
    (2  ,2 ,null,'01/14/2019'), 
    (3  ,2 ,0,'02/03/2019'),
    (4  ,2 ,5,'12/30/2018'),
    (5  ,4 ,3,'01/10/2019'),
    (6  ,3 ,5,'02/14/2019'),
    (7  ,2 ,5,'03/13/2019')
    
    Select X.Id,X.modelId,X.locationId,X.DateCreated from (SELECT *,row_number() OVER (PARTITION by modelId,locationId ORDER BY dateCreated desc)R FROM @DeliveryCarInfo
    
    )X
    WHERE X.R=1
    
    ORDER BY X.modelId,X.DateCreated
    

    【讨论】:

    • 谢谢先生,但它也会删除重复的modelId,即使它的null0 有两个值
    • 了解,已更新我的查询,请使用更新后的代码
    • 采纳为答案,非常感谢先生!这段代码已经卡了一天半了!真的是救命稻草
    【解决方案2】:

    请试试这个。

    SELECT A.* from @tbl A
    INNER JOIN
    (
        SELECT ModelId,Max(DateCreated) As CreateDate from @tbl Group By ModelId
    ) As B
        ON B.ModelId = A.ModelId
        AND A.DateCreated = B.CreateDate
    
     UNION 
    
     Select * from @tbl A
     WHERE LocationId IS NULL OR LocationId = 0
    

    【讨论】:

    • 先生,我如何在您的代码中应用 db.DeliveryCarInfodb.DeliveryDates 的表格。对不起
    • 请替换此(来自)“SELECT ModelId,Max(DateCreated) As CreateDate from tbl Group By ModelId”替换为“SELECT ModelId,Max(DateCreated) As CreateDate from DeliveryCarInfo DCR INNER JOIN DeliveryDates DD ON DCR.Id =DCR.Id Group By DCR.ModelId”并将 tbl 替换为“DeliveryCarInfo”
    【解决方案3】:

    您可以尝试以下查询。联合将有助于获得预期的结果。

    with cte
    as 
    (
    SELECT a.ModelID 
            , a.LocationId 
            , (Select top 1 convert(nvarchar,DateCreated,101) from 
            db.DeliveryDates where isDeleted=0 and DeliveryId=a.DeliveryId) as 
             DateCreated
    FROM db.DeliveryCarInfo a
    WHERE a.isDeleted = 0
    )
    select ModelID, LocationId, DateCreated 
    from cte 
    where isnull(LocationId, 0) = 0
    
    union
    
    select ModelID, LocationId, max(DateCreated) as DateCreated
    from cte
    where isnull(LocationId, 0) != 0
    group by ModelID, LocationId
    

    【讨论】:

    • 先生,我尝试了这个解决方案,但为什么我在 Group By 上获得了ModelID is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.?它
    • 立即尝试更新查询。我无法验证,因为没有准备好表结构和数据。
    • 为什么我的分组中的每一列都是我需要调用的,现在它正在寻找 DateCreated
    • 再试一次...请与一些示例数据共享表结构,以便我可以运行查询并更正语法问题。您要选择的所有列都应该是分组依据或聚合函数的一部分。如果我们有(modelId 和 locationId)的唯一记录,那么我们不需要 group by 和 max 函数。正如我对位置 null 和 0 场景所假设的那样。
    【解决方案4】:

    您可以使用 ROW_NUMBER()

    SELECT ModelID, LocationId, DateCreated FROM  (
        SELECT 
            a.ModelID, a.LocationId,
            CONVERT(NVARCHAR, d.DateCreated, 101) AS DateCreated,
            ROW_NUMBER() OVER(PARTITION BY a.ModelID, a.LocationId ORDER BY d.DateCreated DESC) RN
        FROM 
            db.DeliveryCarInfo a
                LEFT JOIN db.DeliveryDates d ON d.DeliveryId = a.DeliveryId AND d.isDeleted = 0 
        WHERE
            a.isDeleted = 0
    ) AS T WHERE RN = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多