SQL选择具有最新日期的ID

时间:2013-06-20 08:58:09

标签: sql-server

我真的被困了我正在创建一个临时表,其中包含客户列表,最后一个策略签名日期以及业务源的类型。

我正在努力找出成千上万客户的最新业务来源。

DROP TABLE #TEMP

CREATE TABLE #TEMP([user] INT, [policySignedDateTime] DATETIME, [BS] INT)

INSERT INTO #TEMP
SELECT TOP (100) PERCENT
    dbo.tblCustomerUser.IdentityID,
    MAX(dbo.tblApplication.PolicySignedDateTime) AS 'last',
    (dbo.tblApplication.BusinessSourceID) AS BS
FROM dbo.tblApplication
INNER JOIN dbo.tblCustomerUser
    ON dbo.tblApplication.CustomerUserID = dbo.tblCustomerUser.IdentityID
INNER JOIN dbo.tblIndividual
    ON dbo.tblCustomerUser.IdentityID = dbo.tblIndividual.IdentityID
    WHERE (dbo.tblApplication.BusinessSourceID in (1,11))
    AND (dbo.tblApplication.PolicySignedDateTime is not null)
    AND (dbo.tblCustomerUser.IdentityID = 54456)
GROUP BY
    dbo.tblCustomerUser.IdentityID,
    dbo.tblIndividual.FirstNames,
    dbo.tblIndividual.LastName,
    dbo.tblApplication.BusinessSourceID

对于一个用户,输出看起来像这样

  • 54456 // 2012-12-12 00:00:00.000 // 1
  • 54456 // 2012-01-09 00:00:00.000 // 11

所以基本上我只想回到最上一行,因为它是最近的日期。

任何建议都会很棒!

2 个答案:

答案 0 :(得分:2)

使用公用表格式和ROW_NUMBER,例如DENSE_RANKWITH CTE AS ( SELECT dbo.tblcustomeruser.identityid, Max(dbo.tblapplication.policysigneddatetime)OVER( partition BY dbo.tblcustomeruser.identityid, dbo.tblindividual.firstnames, dbo.tblindividual.lastname, dbo.tblapplication.businesssourceid) AS 'last', dbo.tblapplication.businesssourceid AS BS, Row_number() OVER ( partition BY dbo.tblcustomeruser.identityid, dbo.tblindividual.firstnames, dbo.tblindividual.lastname, dbo.tblapplication.businesssourceid ORDER BY dbo.tblapplication.policysigneddatetime DESC) AS RN FROM dbo.tblapplication INNER JOIN dbo.tblcustomeruser ON dbo.tblapplication.customeruserid = dbo.tblcustomeruser.identityid INNER JOIN dbo.tblindividual ON dbo.tblcustomeruser.identityid = dbo.tblindividual.identityid WHERE ( dbo.tblapplication.businesssourceid IN ( 1, 11 ) ) AND ( dbo.tblapplication.policysigneddatetime IS NOT NULL ) AND ( dbo.tblcustomeruser.identityid = 54456 ) ) SELECT * FROM CTE WHERE RN = 1

{{1}}

答案 1 :(得分:0)

我正在使用排名方法 - 建议zx8754。

代码现在看起来像这样

DROP TABLE #TEMP

CREATE TABLE #TEMP([user] INT, [policySignedDateTime] DATETIME, [BS] INT)

INSERT  INTO #TEMP
SELECT        TOP (100) PERCENT dbo.tblCustomerUser.IdentityID, MAX(dbo.tblApplication.PolicySignedDateTime) AS 'last', (dbo.tblApplication.BusinessSourceID) AS BS, rank() OVER (Partition by MAX(dbo.tblCustomerUser.IdentityID) order by MAX(dbo.tblApplication.PolicySignedDateTime)desc) as ranking
FROM            dbo.tblApplication INNER JOIN
                     dbo.tblCustomerUser ON dbo.tblApplication.CustomerUserID = dbo.tblCustomerUser.IdentityID INNER JOIN
                     dbo.tblIndividual ON dbo.tblCustomerUser.IdentityID = dbo.tblIndividual.IdentityID
                     WHERE (dbo.tblApplication.BusinessSourceID in (1,11))
                     and (dbo.tblApplication.PolicySignedDateTime is not null)
                    and (dbo.tblCustomerUser.IdentityID = 54456)
GROUP BY dbo.tblCustomerUser.IdentityID, dbo.tblIndividual.FirstNames, dbo.tblIndividual.LastName, dbo.tblApplication.BusinessSourceID