按日期搜索另一个表中的特定行

时间:2015-06-11 15:35:45

标签: sql sql-server

我有这张桌子:

SELECT * FROM #BH2

BookingID | Detail                                               | CreatedAgentCode | ChangeDate    
----------|------------------------------------------------------|------------------|--------------------------    
196162093 |  MRS LUCIENE CORREA correa MRS LUCIENE  CORREA       | lclisboa         | 2015-01-18 13:29:35.130
196162093 |  MRS LUCIENE CORREA LISBOA MRS LUCIENE CORREA correa | VOMATOS          | 2015-01-18 13:25:26.420

而且:

SELECT * FROM BookingPassengerVersion WHERE BookingID = 196162093  ORDER BY ModifiedDate DESC

BookingID | Title | FirstName | MiddleName | LastName | AgentCode |  ModifiedDate     
----------|-------------------------------------------------------|----------------------------    
196162093 |  MRS  | LUCIENE   |            | CORREA   | lclisboa  | 2015-01-18 13:29:35.130
196162093 |  MRS  | LUCIENE   | CORREA     | correa   | VOMATOS   | 2015-01-18 13:25:26.420
196162093 |  MRS  | LUCIENE   | CORREA     | LISBOA   | ADM       | 2015-01-12 18:01:09.503
196162093 |  MRS  | LUCIENE   | CORREA     | LISBOA   | ADM       | 2015-01-12 18:01:05.227

我需要在old namenew name添加新列:

我尝试了这个查询:

BEGIN TRY DROP TABLE #FINAL_TABLE END TRY BEGIN CATCH END CATCH
SELECT DISTINCT
       BH.BookingID,
       -- S OldName,
       (CASE WHEN _NewName.Title>'' THEN _NewName.Title+' ' ELSE '' END)+_NewName.FirstName+' '+ _NewName.MiddleName+' '+_NewName.LastName AS NewName,            
       BH.CreatedAgentCode, 
       BH.ChangeDate,
INTO   #FINAL_TABLE 
FROM   #BH2 BH

CROSS APPLY
(
     SELECT TOP 2
            Title
            , FirstName
            , MiddleName
            , LastName 
        FROM BookingPassengerVersion
      WHERE BookingID   = BH.BookingID  
        AND BH.ChangeDate = ModifiedDate
     ORDER BY ModifiedDate DESC
) _NewName 

但我无法得到这个结果:

BookingID | OldName                   | NewName                   | Detail                                              | CreatedAgentCode | ChangeDate    
----------|---------------------------|---------------------------|-----------------------------------------------------|------------------|--------------------------    
196162093 | MRS LUCIENE CORREA correa | MRS LUCIENE  CORREA       | MRS LUCIENE CORREA correa MRS LUCIENE  CORREA       | lclisboa         | 2015-01-18 13:29:35.130
196162093 | MRS LUCIENE CORREA LISBOA | MRS LUCIENE CORREA correa | MRS LUCIENE CORREA LISBOA MRS LUCIENE CORREA correa | VOMATOS          | 2015-01-18 13:25:26.420

# BH2包含detail列,此列为Old nameNew name

我需要将old namenew name分成两列,因此我将使用具有更改历史记录的BookingPassengerVersion表。

通过添加titlefirst namemiddle namelast name来形成名称。

3 个答案:

答案 0 :(得分:3)

你可以试试这个。SQL Fiddle

WITH cteBookingPassengerVersion AS
(
    SELECT  BookingID,
            RTRIM(
                CONCAT (
                    ISNULL(Title + ' ', ''),
                    ISNULL(FirstName + ' ', ''),
                    ISNULL(MiddleName + ' ', ''),
                    ISNULL(LastName, '')
                )
            ) AS NAME,
            ModifiedDate,
            ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum
     FROM   BookingPassengerVersion
)
SELECT  cte.BookingID,
        ctePrev.NAME AS OldName,
        cte.NAME AS NewName,
        bh.Detail,
        bh.CreatedAgentCode,
        bh.ChangeDate
FROM    BH2 bh
        JOIN cteBookingPassengerVersion cte ON bh.BookingID = cte.BookingID AND bh.ChangeDate = cte.ModifiedDate
        LEFT JOIN cteBookingPassengerVersion ctePrev ON ctePrev.BookingID = cte.BookingId AND ctePrev.rowNum = cte.rowNum + 1
ORDER BY cte.BookingID, bh.ChangeDate DESC

修改 我更新了查询以加入日期,并获取所有预订的所有更新

更新New SQL Fiddle

要通过BH2中的BookingID过滤CTE,您可以

WITH cteBookingPassengerVersion AS
(
    SELECT  BookingID,
            RTRIM(
                CONCAT (
                  ISNULLLL(Title + ' ', ''),
                    ISNULL(FirstName + ' ', ''),
                    ISNULL(MiddleName + ' ', ''),
                    ISNULL(LastName, '')
                )
            ) AS NAME,
            ModifiedDate,
            ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum
     FROM   BH2
     JOIN   BookingPassengerVersion ON BH2.BookingID = BookingPassengerVersion.BookingID
)

或者

WITH cteBookingPassengerVersion AS
(
    SELECT  BookingID,
            RTRIM(
                CONCAT (
                  ISNULLLL(Title + ' ', ''),
                    ISNULL(FirstName + ' ', ''),
                    ISNULL(MiddleName + ' ', ''),
                    ISNULL(LastName, '')
                )
            ) AS NAME,
            ModifiedDate,
            ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum
     FROM   BookingPassengerVersion
     WHERE  BookingID IN (SELECT BookingID FROM BH2) 
)

在处理大型数据集时,您应该尝试不同的事情。我甚至会用临时表替换cte,看看它是否有帮助。检查你的执行计划,看看你是否还需要任何索引。

临时表而不是cte

SELECT  BookingID,
    RTRIM(
        CONCAT (
            ISNULLLL(Title + ' ', ''),
            ISNULL(FirstName + ' ', ''),
            ISNULL(MiddleName + ' ', ''),
            ISNULL(LastName, '')
        )
    ) AS NAME,
    ModifiedDate,
    ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum
INTO   #bpv
FROM   BookingPassengerVersion
WHERE  BookingID IN (SELECT BookingID FROM BH2) 

SELECT  cte.BookingID,
        ctePrev.NAME AS OldName,
        cte.NAME AS NewName,
        bh.Detail,
        bh.CreatedAgentCode,
        bh.ChangeDate
FROM    BH2 bh
        JOIN #bpv cte ON bh.BookingID = cte.BookingID AND bh.ChangeDate = cte.ModifiedDate
        LEFT JOIN #bpv ctePrev ON ctePrev.BookingID = cte.BookingId AND ctePrev.rowNum = cte.rowNum + 1
ORDER BY cte.BookingID, bh.ChangeDate DESC

答案 1 :(得分:2)

您可以在SqlFiddle中尝试此操作 我更新了user1221684答案以删除重复的行。

WITH cteBookingPassengerVersion AS
(
    SELECT  BookingID,
            RTRIM(
                CONCAT (
                    ISNULL(Title + ' ', ''),
                    ISNULL(FirstName + ' ', ''),
                    ISNULL(MiddleName + ' ', ''),
                    ISNULL(LastName, '')
                )
            ) AS NAME,
            AgentCode, -- add this line
            ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum
     FROM   BookingPassengerVersion WHERE BookingID = 196162093
)
SELECT  cte.BookingID,
        ctePrev.NAME AS OldName,
        cte.NAME AS NewName,
        bh.Detail,
        bh.CreatedAgentCode,
        bh.ChangeDate,
        cte.rowNum,
        ctePrev.rowNum
FROM    BH2 bh
        JOIN cteBookingPassengerVersion cte ON (bh.BookingID = cte.BookingID and bh.CreatedAgentCode = cte.AgentCode) --Update this line
        LEFT JOIN cteBookingPassengerVersion ctePrev ON ctePrev.rowNum = cte.rowNum + 1
WHERE   cte.rowNum <= 2

答案 2 :(得分:0)

对于此架构SqlFiddle,预期结果为:

BookingID | OldName                   | NewName                   | Detail                                              | CreatedAgentCode | ChangeDate    
----------|---------------------------|---------------------------|-----------------------------------------------------|------------------|--------------------------    
196162093 | MRS LUCIENE CORREA correa | MRS LUCIENE  CORREA       | MRS LUCIENE CORREA correa MRS LUCIENE  CORREA       | lclisboa         | 2015-01-18 13:29:35.130
196162093 | MRS LUCIENE CORREA LISBOA | MRS LUCIENE CORREA correa | MRS LUCIENE CORREA LISBOA MRS LUCIENE CORREA correa | VOMATOS          | 2015-01-18 13:25:26.420
195668459 | MR CARLOS PERHARDT JUNIOR | MR CARLOS  PERHARDT       | MR CARLOS PERHARDT JUNIOR MR CARLOS  PERHARDT       | azbussmann       | 2015-01-06 16:36:28.323

EDIT!

最终解决方案,我使用user1221684回答为'BookingPassengerVersion'创建临时表

SELECT * 
    INTO #BPV
    FROM 
         BookingPassengerVersion
WHERE BookingID IN (SELECT DISTINCT BookingID FROM #BH2);

WITH cteBookingPassengerVersion AS
(
    SELECT  BookingID,
            RTRIM(
                CONCAT (
                    ISNULL(Title + ' ', ''),
                    ISNULL(FirstName + ' ', ''),
                    ISNULL(MiddleName + ' ', ''),
                    ISNULL(LastName, '')
                )
            ) AS NAME,
            ModifiedDate,
            ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum
     FROM   #BPV
)
SELECT  cte.BookingID,
        ctePrev.NAME AS OldName,
        cte.NAME AS NewName,
        bh.Detail,
        bh.CreatedAgentCode,
        bh.ChangeDate
FROM    BH2 bh
        JOIN cteBookingPassengerVersion cte ON bh.BookingID = cte.BookingID AND bh.ChangeDate = cte.ModifiedDate
        LEFT JOIN cteBookingPassengerVersion ctePrev ON ctePrev.BookingID = cte.BookingId AND ctePrev.rowNum = cte.rowNum + 1
ORDER BY cte.BookingID, bh.ChangeDate DESC