0

我真的被困住了,我正在创建一个临时表,其中包含客户列表、他们最后的保单签署日期以及业务来源的类型。

我试图找出成千上万客户的最后一个业务来源。

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

所以基本上我试图只返回第一行,因为它是最近的日期。

任何建议都会很棒!

4

2 回答 2

2

使用通用表表达式和窗口函数,如ROW_NUMBERor DENSE_RANK

WITH 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 
于 2013-06-20T09:05:17.257 回答
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
于 2013-06-20T09:14:04.853 回答