将UNION ALL转换为JOIN

时间:2015-04-24 17:56:13

标签: sql sql-server join sql-server-2012 union

有没有办法将UNION ALL转换为JOIN并仍能获得类似的输出。

以下是一个示例查询来说明:

DECLARE @customerIdentifierId BIGINT 
SET @customerIdentifierId = 2

SELECT  1 AS Tag, NULL AS Parent, cust.CustomerId AS CustomerId, 
        NULL AS CustomerIdentifierId, NULL AS OrderDetailId
FROM    Customer.CustomerIdentifier custIdent            
        JOIN Customer.Customer cust
            ON cust.CurrentCustomerIdentifierId = custIdent.CustomerIdentifierId
        JOIN detail.OrderDetail detail
            ON detail.CustomerIdentifierId = custIdent.CustomerIdentifierId
WHERE   custIdent.CustomerIdentifierId = @customerIdentifierId

UNION ALL

SELECT  2, 1, NULL, custIdent.CustomerIdentifierId, null
FROM    Customer.CustomerIdentifier custIdent            
        JOIN Customer.Customer cust
            ON cust.CurrentCustomerIdentifierId = custIdent.CustomerIdentifierId
        JOIN detail.OrderDetail detail
            ON detail.CustomerIdentifierId = custIdent.CustomerIdentifierId
WHERE   custIdent.CustomerIdentifierId = @customerIdentifierId

UNION ALL

SELECT  3, 1, NULL, null, detail.OrderDetailId
FROM    Customer.CustomerIdentifier custIdent            
        JOIN Customer.Customer cust
            ON cust.CurrentCustomerIdentifierId = custIdent.CustomerIdentifierId
        JOIN detail.OrderDetail detail
            ON detail.CustomerIdentifierId = custIdent.CustomerIdentifierId
WHERE   custIdent.CustomerIdentifierId = @customerIdentifierId

空值中的空值并不重要,但是我需要联合给出的单独行。

我尝试过CROSS JOIN,但没有成功。我希望还有其他SQL技巧可以做到(CROSS APPLY?)

如果重要,我的最终目标是让它在SQL Server的索引(物化)视图中工作。

这是我要找的输出:

Tag         Parent      CustomerId           CustomerIdentifierId OrderDetailId
----------- ----------- -------------------- -------------------- --------------------
1           NULL        4                    NULL                 NULL
1           NULL        4                    NULL                 NULL
1           NULL        4                    NULL                 NULL
1           NULL        4                    NULL                 NULL
1           NULL        4                    NULL                 NULL
2           1           NULL                 2                    NULL
2           1           NULL                 2                    NULL
2           1           NULL                 2                    NULL
2           1           NULL                 2                    NULL
2           1           NULL                 2                    NULL
3           2           NULL                 NULL                 2
3           2           NULL                 NULL                 14
3           2           NULL                 NULL                 26
3           2           NULL                 NULL                 38
3           2           NULL                 NULL                 50

这些表是许多孩子关系的父母:

许多CustomerIdentifier的1位客户 1个CustomerIdentifier到许多OrderDetails

(它制作一棵树)

这是创建表以使我的上述查询工作所需的sql的link

3 个答案:

答案 0 :(得分:1)

从未完成索引视图,但您可以重写查询:

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

# END WordPress

答案 1 :(得分:1)

使用变量,以便您可以使用一个选择和一个插入完成整个操作。例如:

DECLARE 
@CustomerId INT,
@OrderDetailId INT;

SELECT
    @CustomerID = cust.CustomerId,
    @OrderDetailID = detail.OrderDetailId
FROM    
    Customer.CustomerIdentifier custIdent
    INNER JOIN Customer.Customer cust ON cust.CurrentCustomerIdentifierId = custIdent.CustomerIdentifierId
    INNER JOIN [order].OrderDetail detail ON detail.CustomerIdentifierId = custIdent.CustomerIdentifierId
WHERE
    detail.CustomerIdentifierId = @customerIdentifierId;

INSERT INTO @xmlDataTable(Tag, Parent, [Customer!1!CustomerId],
            [CustomerIdentifier!2!CustomerIdentifierId], [OrderDetail!3!OrderDetailId])
VALUES
(1, NULL, @CustomerID , NULL, NULL),
(2, 1, NULL, @customerIdentifierId, NULL),
(3, 1, NULL, null, @OrderDetailID);

答案 2 :(得分:1)

这里有一些伪造的伪代码,你可以做到这一点:

SELECT COALESCE(t1.Col1, t2.Col1, tN.Col1) AS Col1
, COALESCE(t1.Col2, t2.Col2, tN.Col2) AS Col2
, COALESCE(t1.ColN, t2.ColN, tN.ColN) AS ColN
FROM t1
FULL OUTER JOIN t2 ON 1=0
FULL OUTER JOIN tN ON 1=0

我还没有探究这是否会违反索引视图的要求,但这是一种可以使用JOIN复制UNION ALL结果的方法。