在 SQL 服务器中的最新日期获取 Distinct Id(FK)

Getting Distinct Id(FK) by the latest date in SQL Server

我目前正在处理一个查询,我需要根据最近创建的日期来区分 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

您可以尝试以下查询。联盟将帮助获得理​​想的结果。

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

请试试这个。

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

您可以使用 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

请尝试使用以下代码

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