sql - Convert UNION ALLs to JOINs -


is there way convert union all join , still similar output.

here example query illustrate:

declare @customeridentifierid bigint  set @customeridentifierid = 2  select  1 tag, null parent, cust.customerid customerid,          null customeridentifierid, null orderdetailid    customer.customeridentifier custident                     join customer.customer cust             on cust.currentcustomeridentifierid = custident.customeridentifierid         join detail.orderdetail detail             on detail.customeridentifierid = custident.customeridentifierid   custident.customeridentifierid = @customeridentifierid  union  select  2, 1, null, custident.customeridentifierid, null    customer.customeridentifier custident                     join customer.customer cust             on cust.currentcustomeridentifierid = custident.customeridentifierid         join detail.orderdetail detail             on detail.customeridentifierid = custident.customeridentifierid   custident.customeridentifierid = @customeridentifierid  union  select  3, 1, null, null, detail.orderdetailid    customer.customeridentifier custident                     join customer.customer cust             on cust.currentcustomeridentifierid = custident.customeridentifierid         join detail.orderdetail detail             on detail.customeridentifierid = custident.customeridentifierid   custident.customeridentifierid = @customeridentifierid 

it not important nulls have null in them, need separate rows union gives.

i tried doing cross join, , did not work out. hoping there other sql trick can (cross apply?)

in case matters, end goal work in indexed (materialized) view in sql server.

this output looking for:

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 

the tables parent many children relationship:

1 customer many customeridentifiers 1 customeridentifier many orderdetails

(it makes tree)

here link sql needed create tables make above query work:

never done indexed view rewrite query:

insert @xmldatatable(tag, parent, [customer!1!customerid],             [customeridentifier!2!customeridentifierid], [orderdetail!3!orderdetailid])  select  rows.*    customer.customeridentifier custident                     join customer.customer cust             on cust.currentcustomeridentifierid = custident.customeridentifierid         join [order].orderdetail detail             on detail.customeridentifierid = custident.customeridentifierid   detail.customeridentifierid = @customeridentifierid outer apply (    select 1, null, cust.customerid, null, null    union    select  2, 1, null, custident.customeridentifierid, null    union    select  3, 1, null, null, detail.orderdetailid ) rows 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -