T-SQL将两行合并并合并为一行

时间:2015-12-08 13:13:27

标签: sql sql-server tsql

目标是让安装地址和调度地址出现在同一行,但我无法找到实现此目的的方法。我正在对相同的数据进行2次查询,并在结果上进行联合。地址详细信息可能不同,但也可能相同(例如,安装和发送地址相同)。

data needs merging into one row

Select  zSTRI_CertificateNumber, CPS, InstallAdr1, InstallCity, DispAdr1, DispCity, DateSubmitted
From (

    SELECT zSTRI_CertificateNumber,
            'STRI' + CAST(op.ID as Varchar(4)) as CPSref,
            JobRef,
            CAST(CASE
                WHEN notif.Cps = 1 THEN 'CPS'
                END AS varchar(3)) as CPS,
            notif.DateSubmitted,
            nAdr.AddressLine1 as InstallAdr1,
            nAdr.AddressLine2 as InstallAdr2,
            nAdr.City as InstallCity,
            nAdr.PostCode as InstallPostCode,
            '' as DispAdr1,
            '' as DispAdr2,
            '' as DispCity,
            '' as DispPostCode,

            DateWorkCompleted,
            c.CompanyName,
            msrs.UniqueID

    FROM [Notification] notif 
        INNER JOIN NotificationAddress nAdr
            ON notif.ID = nAdr.NotificationID
        INNER JOIN Company c
            ON c.CompanyID = notif.CompanyID
        INNER JOIN NotificationMeasures msrs
            ON notif.ID = msrs.NotificationID
        INNER JOIN Operative op
            ON op.ID = NotifyingOperativeID
    WHERE  notif.DispatchMethodEmail = 0
            AND nAdr.InstallAddress = 1
             AND notif.ID = 5411
        UNION ALL


    SELECT zSTRI_CertificateNumber,
            'STRI' + CAST(op.ID as Varchar(4)) as CPSref,
            JobRef,
            CAST(CASE
                WHEN notif.Cps = 1 THEN 'CPS'
                END AS varchar(3)) as CPS,
            notif.DateSubmitted,
            '' as InstallAdr1,
            '' as InstallAdr2,
            '' as InstallCity,
            '' as InstallPostCode,

            nAdr.AddressLine1 as DispAdr1,
            nAdr.AddressLine2 as DispAdr2,
            nAdr.City as DispCity,
            nAdr.PostCode as DispPostCode,
            DateWorkCompleted,
            c.CompanyName,
            msrs.UniqueID

    FROM [Notification] notif 
        INNER JOIN NotificationAddress nAdr
            ON notif.ID = nAdr.NotificationID
        INNER JOIN Company c
            ON c.CompanyID = notif.CompanyID
        INNER JOIN NotificationMeasures msrs
            ON notif.ID = msrs.NotificationID
        INNER JOIN Operative op
            ON op.ID = NotifyingOperativeID
    WHERE
            notif.DispatchMethodEmail = 0
            AND nAdr.DispatchAddress = 1 
            AND notif.ID = 5411
) as SubGroup
Group by  zSTRI_CertificateNumber, CPS, InstallAdr1, InstallCity, DispAdr1, DispCity, DateSubmitted

3 个答案:

答案 0 :(得分:1)

可能是你的代码是如此巨大,根据我的假设和使用MAX值为一些NULL列并删除它们在Group BY

SELECT 
 zSTRI_CertificateNumber, 
 CPS, 
 MAX(InstallAdr1) InstallAdr1, 
InstallCity, 
MAX(DispAdr1)DispAdr1,
 MAX(DispCity)DispCity, 
DateSubmitted

            FROM (
     SELECT zSTRI_CertificateNumber,
                'STRI' + CAST(op.ID as Varchar(4)) as CPSref,
                JobRef,
                CAST(CASE
                    WHEN notif.Cps = 1 THEN 'CPS'
                    END AS varchar(3)) as CPS,
                notif.DateSubmitted,
                nAdr.AddressLine1 as InstallAdr1,
                nAdr.AddressLine2 as InstallAdr2,
                nAdr.City as InstallCity,
                nAdr.PostCode as InstallPostCode,
                '' as DispAdr1,
                '' as DispAdr2,
                '' as DispCity,
                '' as DispPostCode,

                DateWorkCompleted,
                c.CompanyName,
                msrs.UniqueID

        FROM [Notification] notif 
            INNER JOIN NotificationAddress nAdr
                ON notif.ID = nAdr.NotificationID
            INNER JOIN Company c
                ON c.CompanyID = notif.CompanyID
            INNER JOIN NotificationMeasures msrs
                ON notif.ID = msrs.NotificationID
            INNER JOIN Operative op
                ON op.ID = NotifyingOperativeID
        WHERE  notif.DispatchMethodEmail = 0
                AND nAdr.InstallAddress = 1
                 AND notif.ID = 5411
            UNION ALL


        SELECT zSTRI_CertificateNumber,
                'STRI' + CAST(op.ID as Varchar(4)) as CPSref,
                JobRef,
                CAST(CASE
                    WHEN notif.Cps = 1 THEN 'CPS'
                    END AS varchar(3)) as CPS,
                notif.DateSubmitted,
                '' as InstallAdr1,
                '' as InstallAdr2,
                '' as InstallCity,
                '' as InstallPostCode,

                nAdr.AddressLine1 as DispAdr1,
                nAdr.AddressLine2 as DispAdr2,
                nAdr.City as DispCity,
                nAdr.PostCode as DispPostCode,
                DateWorkCompleted,
                c.CompanyName,
                msrs.UniqueID

        FROM [Notification] notif 
            INNER JOIN NotificationAddress nAdr
                ON notif.ID = nAdr.NotificationID
            INNER JOIN Company c
                ON c.CompanyID = notif.CompanyID
            INNER JOIN NotificationMeasures msrs
                ON notif.ID = msrs.NotificationID
            INNER JOIN Operative op
                ON op.ID = NotifyingOperativeID
        WHERE
                notif.DispatchMethodEmail = 0
                AND nAdr.DispatchAddress = 1 
                AND notif.ID = 5411

            )As Subgroup
            GROUP BY zSTRI_CertificateNumber, CPS, InstallCity, DateSubmitted

答案 1 :(得分:1)

听起来您只需要使用不同的连接条件在单个查询中连接两次相同的NotificationAddress表。

e.g。

select A.id, X.value as 'xValue', Y.value as 'yValue'
from IdTable A
inner join ValueTable X
    on A.id=X.id
inner join ValueTable Y      -- same table as "X"
    on A.id=Y.id
where X.type = 'X'
and   Y.type = 'Y'           -- but different join criteria

答案 2 :(得分:0)

以下是完成工作的查询的摘要版本(基于接受的答案)

Select NotifId, MAX(InstallAddress1) InstallAddress1, 
        MAX(InstallAddress2) InstallAddress2,
        MAX(InstallCity) InstallCity,
        MAX(InstallPostCode) InstallPostCode,
        MAX(DispatchAddress1) DispatchAddress1,
        MAX(DispatchAddress2) DispatchAddress2,
        MAX(DispatchCity) DispatchCity,
        MAX(DispatchPostCode) DispatchPostCode
    FROM (
        select  X.NotificationID as NotifId,
            X.AddressLine1 as 'InstallAddress1',
            X.AddressLine2 as 'InstallAddress2',
            X.City as 'InstallCity',
            X.Postcode as 'InstallPostCode',
            null as 'DispatchAddress1',
            null as 'DispatchAddress2',
            null as 'DispatchCity',
            null as 'DispatchPostCode'
        from NotificationAddress X
        Where X.InstallAddress = 1

        UNION
        Select Y.NotificationID as NotifId,
            null as 'InstallAddress1',
            null as 'InstallAddress2',
            null as 'InstallCity',
            null as 'InstallPostCode',
            Y.AddressLine1 as 'DispatchAddress1',
            Y.AddressLine2 as 'DispatchAddress2',
            Y.City as 'DispatchCity',
            Y.Postcode as 'DispatchPostCode'
            from NotificationAddress Y
        where   Y.DispatchAddress = 1
 ) as b
    GROUP BY  NotifId