SQL:仅返回多个最旧的记录

时间:2015-04-14 16:33:50

标签: sql-server

我正在尝试使用一个复杂的sql返回一个字符串,但我不能让它基于与最新的D.CreateDateTime相同的phonenumber返回,例如

Source           CallData                       Phonenum    FK_REF  D.createdatetime
Source   1 609 ^Mr Richard Smith^01234567891^   01234567891 27657   16/06/2014
Source2  1 609 ^Mr Richard Smith^01234567891^   01234567891 27657   21/07/2014
Source3  1 609 ^Mr Richard Smith^01234567891^   01234567891 27657   03/10/2014

预期结果

    Source           CallData                       Phonenum    FK_REF  D.createdatetime
    Source3  1 609 ^Mr Richard Smith^01234567891^   01234567891 27657   03/10/2014

然而,我似乎无法找到一种方法来有效地做到这一点,尝试使用

SQL Server: SELECT only the rows with MAX(DATE)T-SQL select rows by oldest date and unique category

不起作用,或者说我找不到将所述数据插入查询的地方。

如果有人能够理解(有点复杂的)sql查询足以提供帮助,那就太棒了。

谢谢

以下代码

--Create Table #tmp2(FK_clientids varchar(50))
--Create table #tmp (phonenums varchar(50))
Delete from #tmp2
Delete from #tmp
Use DavikerTWF_MTA
INSERT INTO #tmp2
SELECT fk_clientid
FROM DM_ClientApplicants
where FK_ApplicationID in (--FK_ApplicationID goes here)

Use DavikerTWF_OTS
INSERT INTO #tmp
Select phonenum2 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select PhoneNum1 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select PhoneNum2 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)

INSERT INTO #tmp
Select PhoneNum3 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select Partnerphonehome from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select Partnerphonemobile from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select Partnerphonework from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
Use TouchStar
Select sourcetable,CallData,PhoneNum,DM_PhoneNumbers.FK_ApplicationID,d.CreateDateTime from Dial D
join DavikerTWF_OTS.dbo.DM_PhoneNumbers on PhoneNum= PhoneNum1
Collate latin1_general_CI_AS
where PhoneNum in
(
Select phonenums from #tmp
)

2 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点。我经常这样做的方法是使用row_number函数来排序您感兴趣的键分区的行,然后只选择最上面的行。像这样:

select sourcetable,CallData,PhoneNum,FK_ApplicationID,CreateDateTime
from
(
    Select sourcetable,CallData,PhoneNum,DM_PhoneNumbers.FK_ApplicationID,d.CreateDateTime, ROW_NUMBER() Over (Partition by PhoneNum order by d.CreateDateTime desc) rownum
    from Dial D
    join DavikerTWF_OTS.dbo.DM_PhoneNumbers 
    on PhoneNum= PhoneNum1 Collate latin1_general_CI_AS
    where PhoneNum in
        (
        Select phonenums from #tmp
        )
) AllRows
where rownum = 1

答案 1 :(得分:1)

尝试这样的事情......

;WITH CTE AS 
(SELECT *
      ,ROW_NUMBER() OVER (PARTITION BY Phonenum ORDER BY
              CAST(RIGHT(createdatetime ,4)+SUBSTRING(createdatetime, 4,2)+LEFT(createdatetime ,2) 
                   AS DATETIME) DESC) rn
FROM TableName   
)
SELECT * FROM CTE 
WHERE rn = 1