动态比较SQL中的记录

时间:2016-04-05 18:04:03

标签: sql pivot unpivot

根据ID,我需要检查大约15个表,以查找不匹配的记录。所以,例如,我有一个联系信息表:

TransID FirstName   LastName    Phone           Extn
80071   Michael     Kriegel     888-555-6265    1111
80071   Pam         Morrow      888-555-9999    2222
80071   Barbara     Smith       888-555-7770    3333
80071   Yessika     Zurita      888-555-6622    4444
80072   Pam         Morrow      888-555-9999    2222
80072   Barbara     Smith       888-555-7777    3333
80072   Yessika     Zurita      888-555-9954    4444
80072   Michael     Kriegel     888-555-6265    1122

我们有Michael Kriegel,他的Extn在两个TransID之间是不同的,我们有Barbara Smith和Zurita Yesskia,他们的电话号码不同。

我需要做的是找出一种方法来显示哪些记录不同,并显示哪些字段是不同的。但我也需要写这个,所以它是通用的,因为我不想硬编码15种不同的比较。

我希望我的输出看起来像这样:

Name                  Field         TransID_80071   TransID_80072
Michael Kriegel       Extn          1111            1122
Barbara Smith         Phone         888-555-7770    888-555-7777
Zurita Yessika        Phone         888-555-6622    888-555-9954

我正在搞乱UNPIVOT,但我无法到达那里。我有这个代码:

SELECT TransID , Value , FieldName
FROM 
   (SELECT Max(TransID) as TransID, Convert(VarChar(250),FirstName) as FirstName, 
       Convert(VarChar(250),LastName) as LastName, 
       Convert(VarChar(250),Phone) as Phone, Convert(VarChar(250),CoExtn) as CoExtn
   FROM tblSQLContacts
   WHERE TransID IN ('80071', '80072')
   GROUP BY FirstName, LastName, Phone, CoExtn 
   HAVING count(*) < 2   
   ) p
UNPIVOT
   (FieldName  FOR Value IN 
      (FirstName, LastName, Phone, CoExtn)
)AS unpvt

这给了我一个结果:

TransID Value         FieldName
80071   FirstName     Barbara
80071   LastName      Smith
80071   Phone         888-555-7770
80071   Extn          3333
80072   FirstName     Barbara
80072   LastName      Smith
80072   Phone         888-555-7777
80072   Extn          3333
80071   FirstName     Michael
80071   LastName      Kriegel
80071   Phone         888-555-6265
80071   Extn          1111
80072   FirstName     Michael
80072   LastName      Kriegel
80072   Phone         888-555-6265
80072   Extn          1122
80071   FirstName     Yessika
80071   LastName      Zurita
80071   Phone         888-555-6622
80071   Extn          4444
80072   FirstName     Yessika
80072   LastName      Zurita
80072   Phone         888-555-9954
80072   Extn          4444

所以,我正在提取正确的记录,但我无法弄清楚如何将其变成我想要的格式。有人能让我超越终点线吗?

1 个答案:

答案 0 :(得分:1)

如果您有这样的数据..

CREATE TABLE #tSQLContacts
    ([TransID] int, [FirstName] varchar(250), [LastName] varchar(250), [Phone] varchar(250), [Extn] varchar(250))
;

INSERT INTO #tSQLContacts
    ([TransID], [FirstName], [LastName], [Phone], [Extn])
VALUES
    (80071, 'Michael', 'Kriegel', '888-555-6265', '1111'),
    (80071, 'Pam', 'Morrow', '888-555-9999', '2222'),
    (80071, 'Barbara', 'Smith', '888-555-7770', '3333'),
    (80071, 'Yessika', 'Zurita', '888-555-6622', '4444'),
    (80072, 'Pam', 'Morrow', '888-555-9999', '2222'),
    (80072, 'Barbara', 'Smith', '888-555-7777', '3333'),
    (80072, 'Yessika', 'Zurita', '888-555-9954', '4444'),
    (80072, 'Michael', 'Kriegel', '888-555-6265', '1122')
;

你可以得到你想要的结果。

;WITH cte AS (
    SELECT  *, 
            DENSE_RANK() OVER (PARTITION BY Rnk1, ColumnName ORDER BY ColumnValue) Rnk2  
    FROM (
        SELECT  *,
                DENSE_RANK() OVER (ORDER BY [FirstName], [LastName]) Rnk1
        FROM    #tSQLContacts
        WHERE   TransID IN ('80071', '80072')
    ) t
    UNPIVOT
    (
        ColumnValue
        FOR ColumnName IN ([Phone], [Extn] )
    ) up
)
SELECT CONCAT(FirstName, ' ',LastName) AS NAME,
            ColumnName AS Field,
            [80071] AS [TransID_80071],
            [80072] AS [TransID_80072]
FROM (
    SELECT  FirstName,
            LastName,
            ColumnName,
            ColumnValue,
            TransID
    FROM    cte t1
    WHERE   EXISTS ( SELECT *
                     FROM   cte t2
                     WHERE  t1.Rnk1 = t2.Rnk1
                            AND t1.ColumnName = t2.ColumnName
                            AND t2.Rnk2 > 1 )
) t
PIVOT
(
    MAX(ColumnValue) 
    FOR TransID IN ([80071], [80072])
) p 

这很可能是动态完成的,但我不确定我们是否有足够的信息为您做到这一点。