SQL:如何在选择多个记录时仅选择最新记录

时间:2015-04-27 12:31:43

标签: sql sql-server

所以我想接受这个"返回"根据创建的日期是最年轻的(最新的)来表示最近的添加结果并将其转换为下面的结果。

PhoneNum    sourcetable      FullName         reference        Task                                         CreatedDate
    0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  16/03/2015 15:01:05
    0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  12/03/2015 16:58:22
    0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  12/03/2015 16:58:25
    0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  10/03/2015 12:29:50
    0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  12/03/2015 14:18:47
    0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  10/03/2015 12:40:21
    1   This is not important   Mr wilson smith 39158   This is different every time, but has been renamed  10/03/2015 12:07:14
    1   This is not important   Mr wilson smith 39158   This is different every time, but has been renamed  10/03/2015 12:07:14
    1   This is not important   Mr wilson smith 39158   This is different every time, but has been renamed  10/03/2015 12:07:13

我需要返回的内容:

PhoneNum    sourcetable     FullName       reference      Task                                           CreatedDate
0   This is not important   Mr john smith   39161   This is different every time, but has been renamed  12/03/2015 16:58:25
1   This is not important   Mr wilson smith 39158   This is different every time, but has been renamed  10/03/2015 12:07:14

到目前为止我有什么

select distinct d.PhoneNum,d.sourcetable,N.FullName,C.fk_applicationid as ref,t.Subject,t.CreatedDate 
from Dial d
join Database.dbo.DM_PhoneNumbers p on p.PhoneNum1 = d.PhoneNum collate latin1_general_CI_AS
join Database.dbo.DM_PhoneNumbers on p.PhoneNum2 = d.PhoneNum collate latin1_general_CI_AS
join Database.dbo.DM_ClientApplicants C on C.FK_ClientID = P.FK_ApplicationID
join Database.dbo.DM_Names N on c.FK_ClientID = N.FK_ApplicationID
join Database.dbo.Tasks T on T.FK_ApplicationID = c.FK_ApplicationID
where c.FK_ClientID in (39157,39160)

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:8)

您可以使用ROW_NUMBER()

;WITH CTE AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY REFERENCE ORDER BY CREATEDDATE DESC) AS RN
FROM [TABLE])
SELECT *
FROM CTE
WHERE RN = 1

显然,您只需更改select语句即可获得所需的列。

答案 1 :(得分:2)

请使用等级功能查找旧记录,这是未经测试的!!希望这个帮助

SELECT * FROM (

    select distinct d.PhoneNum,d.sourcetable,N.FullName,C.fk_applicationid as ref,t.Subject,t.CreatedDate 
    , RANK() OVER ( PARTITION BY  N.FullName ORDER BY t.CreatedDate DESC ) AS iRank


    from Dial d
    join Database.dbo.DM_PhoneNumbers p on p.PhoneNum1 = d.PhoneNum collate latin1_general_CI_AS
    join Database.dbo.DM_PhoneNumbers on p.PhoneNum2 = d.PhoneNum collate latin1_general_CI_AS
    join Database.dbo.DM_ClientApplicants C on C.FK_ClientID = P.FK_ApplicationID
    join Database.dbo.DM_Names N on c.FK_ClientID = N.FK_ApplicationID
    join Database.dbo.Tasks T on T.FK_ApplicationID = c.FK_ApplicationID
    where c.FK_ClientID in (39157,39160)

) AS t 
WHERE t.iRank = 1

答案 2 :(得分:0)

添加"按顺序排列"到最后得到订单" desc"最新的

ORDER BY t.CreatedDate DESC

将组添加到最后

添加" top"刚刚选择后限制返回的记录数

SELECT TOP 1 ...

好的,再试一次

SELECT 
    person.PhoneNum,
    person.sourcetable,
    person.FullName,
    person.ref,

    t.Subject,t.CreatedDate 

FROM Database.dbo.Tasks t
JOIN (
    SELECT d.PhoneNum,d.sourcetable,N.FullName,C.fk_applicationid as ref
    FROM Dial d
    JOIN Database.dbo.DM_PhoneNumbers p on p.PhoneNum1 = d.PhoneNum collate latin1_general_CI_AS
    JOIN Database.dbo.DM_PhoneNumbers on p.PhoneNum2 = d.PhoneNum collate latin1_general_CI_AS
    JOIN Database.dbo.DM_ClientApplicants C on C.FK_ClientID = P.FK_ApplicationID
    JOIN Database.dbo.DM_Names N on c.FK_ClientID = N.FK_ApplicationID

    WHERE c.FK_ClientID in (39157,39160)

) person ON T.FK_ApplicationID = person.ref

ORDER BY t.CreatedDate DESC

在这里,我先选择最新的任务,然后加入每个人只有一个人的选定人员

我没有你的数据库,希望它有效或给你正确的引导

答案 3 :(得分:0)

你只需要用我添加的最后一行来改变它。仅为每个客户选择最近的日期:

select distinct d.PhoneNum,d.sourcetable,N.FullName,C.fk_applicationid as ref,t.Subject,t.CreatedDate 
from Dial d
join Database.dbo.DM_PhoneNumbers p on p.PhoneNum1 = d.PhoneNum collate latin1_general_CI_AS
join Database.dbo.DM_PhoneNumbers on p.PhoneNum2 = d.PhoneNum collate latin1_general_CI_AS
join Database.dbo.DM_ClientApplicants C on C.FK_ClientID = P.FK_ApplicationID
join Database.dbo.DM_Names N on c.FK_ClientID = N.FK_ApplicationID
join Database.dbo.Tasks T on T.FK_ApplicationID = c.FK_ApplicationID
where c.FK_ClientID in (39157,39160)
AND CreatedDate >= ALL (SELECT DISTINCT MAX(CreatedDate) FROM Tasks WHERE PhoneNum = d.PhoneNum)

通过这种方式,您将获得所有客户,但只能获得每个客户的最后一行。