从2个表中选择数据,在这些表中扩展行

时间:2017-10-12 08:02:49

标签: sql sql-server backup

我的问题是:

我有两张桌子。一个携带真实数据,另一个用作备份。每当真实数据发生变化时,触发器就会将原始行复制到备份表中。

我需要的是以下内容:

我想选择所有原始的真实数据。这意味着真实数据表中的所有条目从未被更改,并且备份表中的所有数据都是第一次插入的。

想象一下这些表:

╔════════╦══════════╗ ╔══════════╦════════╦══════════╗
║ RealId ║ Numeric  ║ ║ BackupId ║ RealId ║ Numeric  ║
╠════════╬══════════╣ ╠══════════╬════════╬══════════╣
║      1 ║        3 ║ ║        1 ║      1 ║        7 ║
║      2 ║       19 ║ ║        2 ║      1 ║        9 ║
║      3 ║       24 ║ ║        3 ║      1 ║       14 ║
║      4 ║        5 ║ ║        4 ║      2 ║        2 ║
║      5 ║       23 ║ ║        5 ║      3 ║       13 ║
╚════════╩══════════╝ ║        6 ║      5 ║        9 ║
                      ║        7 ║      5 ║        4 ║
                      ╚══════════╩════════╩══════════╝

我的目标是创建一个返回如下内容的查询:

╔══════════╦════════╦══════════╗
║ BackupId ║ RealId ║ Numeric  ║
╠══════════╬════════╬══════════╣
║ 1        ║      1 ║        7 ║
║ 4        ║      2 ║        2 ║
║ 5        ║      3 ║       13 ║
║ NULL     ║      4 ║        5 ║
║ 6        ║      5 ║        9 ║
╚══════════╩════════╩══════════╝

如您所见,我总是希望从备份表返回使用特定Realid创建的第一个条目。 (RealId和BackupId始终是唯一的)

我想到的一种方法是

SELECT MIN(BackupId), RealId
FROM BackupTable
GROUP BY RealId

至于真实桌子,我想到了像

这样的东西
SELECT *
FROM real-table A
WHERE NOT EXISTS (
     SELECT * 
     FROM backup-table B
     WHERE B.RealId = A.RealId
)

但我无法想出合并表格的正确方法。

3 个答案:

答案 0 :(得分:1)

使用窗口功能:

Create table #Real (RealID int, [Numeric] int)

Create table #Backup (BackupID int, RealID int, [Numeric] int)

Insert into #Real values(1, 3)
Insert into #Real values(2, 19)
Insert into #Real values(3, 24)
Insert into #Real values(4, 5)
Insert into #Real values(5, 23)

Insert into #Backup values (1, 1, 7)
Insert into #Backup values (2, 1, 9)
Insert into #Backup values (3, 1, 14)
Insert into #Backup values (4, 2, 2)
Insert into #Backup values (5, 3, 13)
Insert into #Backup values (6, 5, 9)
Insert into #Backup values (7, 5, 4)

Select distinct 
First_Value(b.[BackupID]) over (partition by r.RealID order by b.BackupID) as [BackupID]
    , r.RealID as [RealID]
    , isnull(First_Value(b.[Numeric]) over (partition by r.RealID order by b.BackupID), r.numeric) as [Numeric]
from #Real r 
left join #Backup b on r.realID = b.realid
order by r.[RealID]

答案 1 :(得分:1)

对于2012年之前的任何人......

(*现在编辑以假设所有realID都将存在于真实表中)

SELECT        
    firstB.minBU AS [first Backup]
    , R.realid
    , ISNULL(B.numeric, R.numeric) AS [original value]
FROM            
    (SELECT realid, MIN(backupid) AS minBU
        FROM test.[backup] AS backup_1 GROUP BY realid
    ) AS firstB 
    INNER JOIN
    test.[backup] AS B 
        ON firstB.realid = B.realid AND firstB.minBU = B.backupid 
    RIGHT OUTER JOIN
        test.real AS R ON firstB.realid = R.realid

答案 2 :(得分:1)

我在SO上发现了这篇文章:

Select from table if record found in another table

这个问题的答案帮助我制定了这样的解决方案:

SELECT NULL AS BackupId, A.* FROM real-table A
WHERE NOT EXISTS (
    SELECT * 
    FROM backup-table B
    WHERE B.RealId = A.RealId
)

union all

Select C.* FROM backup-table C
INNER JOIN (
    SELECT MIN(BackupId) AS BackupId, 
           RealId
        FROM backup-table 
        GROUP BY RealId 
    ) D
on D.BackupId = C.BackupId

ORDER BY RealId Asc

但是,我想针对cloudsafes回答测试这个,看看哪个有更好的性能。