合并查询

Merging queries

我有两个查询效果很好,但我需要合并这两个结果,但到目前为止还不能。就好像我需要调整次要结果,但它超出了我的知识范围。

第一个查询收集了所有详细信息(为简洁起见,省略了许多列)。如果备用代码可用,第二个获取备用帐单代码以填充到任何 NULL 或 EMPTY MisCptID 字段中。

因此,如果 MisCptID_01 和 MisCptID_02 有数据,它们将保持原样,但 3 - 6 需要使用备用 CPT 的可用列表。如果没有替代者,则它们保持 NULL 或 EMPTY。

第一个查询的结果(样本):

ID              Location    MisCptID_01     MisCptID_02     MisCptID_03     MisCptID_04     MisCptID_05     MisCptID_06
AXXXXXXXX9443   OXXXXXOR    43248           43239           NULL            NULL            NULL            NULL
AXXXXXXXX1163   EXXXXXED    43249           43247           43239           NULL            NULL            NULL
AXXXXXXXX1765   OXXXXXOR    43251           45385           45382           45384           45380           45381

包含备用代码(示例)的第二个查询的结果:

ID              AlternateCodeCode   StandardAmount
AXXXXXXXX9443   80048               49.00
AXXXXXXXX9443   81001               38.00
AXXXXXXXX9443   76000               0.00
AXXXXXXXX1163   84703               80.50
AXXXXXXXX1163   82040               21.70
AXXXXXXXX1163   83036               49.00
AXXXXXXXX1163   85014               10.90
AXXXXXXXX1163   85018               10.90
AXXXXXXXX1163   86901               18.10
AXXXXXXXX1765   88305               185.00
AXXXXXXXX1765   88311               32.00
AXXXXXXXX1765   93005               125.00
AXXXXXXXX1765   80048               49.00
AXXXXXXXX1765   85027               10.80

合并查询结果所需的结果:

ID              Location    MisCptID_01     MisCptID_02     MisCptID_03     MisCptID_04     MisCptID_05     MisCptID_06
AXXXXXXXX9443   OXXXXXOR    43248           43239           80048           81001           76000           NULL
AXXXXXXXX1163   EXXXXXED    43249           43247           43239           84703           83036           86901
AXXXXXXXX1765   OXXXXXOR    43251           45385           45382           45384           45380           45381

您可以反转第一个查询,将结果与第二个查询合并,然后再次将它们转回原位,如下所示:

-- sample data:

DECLARE @ResultsOfFirstQuery TABLE (
    ID VARCHAR(20) NOT NULL,
    Location VARCHAR(10) NOT NULL,
    MisCptID_01 INT NULL,
    MisCptID_02 INT NULL,
    MisCptID_03 INT NULL,
    MisCptID_04 INT NULL,
    MisCptID_05 INT NULL,
    MisCptID_06 INT NULL
)

INSERT INTO @ResultsOfFirstQuery (ID, Location, MisCptID_01, MisCptID_02, MisCptID_03, MisCptID_04, MisCptID_05, MisCptID_06) VALUES
('AXXXXXXXX9443','OXXXXXOR',43248,43239,NULL ,NULL ,NULL ,NULL ),
('AXXXXXXXX1163','EXXXXXED',43249,43247,43239,NULL ,NULL ,NULL ),
('AXXXXXXXX1765','OXXXXXOR',43251,45385,45382,45384,45380,45381)

DECLARE @ResultsOfSecondQuery TABLE (
    ID VARCHAR(20) NOT NULL,
    AlternateCodeCode INT NOT NULL,
    StandardAmount NUMERIC(10,2) NOT NULL
)

INSERT INTO @ResultsOfSecondQuery (ID, AlternateCodeCode, StandardAmount) VALUES
('AXXXXXXXX9443',80048,49.00 ),
('AXXXXXXXX9443',81001,38.00 ),
('AXXXXXXXX9443',76000,0.00  ),
('AXXXXXXXX1163',84703,80.50 ),
('AXXXXXXXX1163',82040,21.70 ),
('AXXXXXXXX1163',83036,49.00 ),
('AXXXXXXXX1163',85014,10.90 ),
('AXXXXXXXX1163',85018,10.90 ),
('AXXXXXXXX1163',86901,18.10 ),
('AXXXXXXXX1765',88305,185.00),
('AXXXXXXXX1765',88311,32.00 ),
('AXXXXXXXX1765',93005,125.00),
('AXXXXXXXX1765',80048,49.00 ),
('AXXXXXXXX1765',85027,10.80 )

-- query:

SELECT p.ID, q1.Location, p.[1] AS MisCptID_01, p.[2] AS MisCptID_02, p.[3] AS MisCptID_03, p.[4] AS MisCptID_04, p.[5] AS MisCptID_05, p.[6] AS MisCptID_06
FROM (
    SELECT x.ID, x.Code, ROW_NUMBER() OVER (PARTITION BY x.ID ORDER BY x.SourceQuery, x.Position) AS Position
    FROM (
        SELECT u.ID, u.Code, 1 AS SourceQuery, RIGHT(u.ColumnName,2) AS Position
        FROM (
            SELECT ID, MisCptID_01, MisCptID_02, MisCptID_03, MisCptID_04, MisCptID_05, MisCptID_06
            FROM @ResultsOfFirstQuery
        ) q
        UNPIVOT (Code FOR ColumnName IN (MisCptID_01, MisCptID_02, MisCptID_03, MisCptID_04, MisCptID_05, MisCptID_06)) u
        UNION ALL
        SELECT ID, AlternateCodeCode, 2 AS SourceQuery, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Position
        FROM @ResultsOfSecondQuery
    ) x
) y
PIVOT (MAX(Code) FOR Position IN ([1],[2],[3],[4],[5],[6])) p
INNER JOIN @ResultsOfFirstQuery q1 ON q1.ID = p.ID