2016-11-29 84 views
0

我有一個查詢,像這樣:如何獲得結果從UNION的子集所有查詢結果

SELECT Temp.description FROM (
    (
     SELECT Clients.aliasname AS description 
     FROM ClientBilling 
     INNER JOIN [B2BSetups].[dbo].Clients ON Clients.cno = ClientBilling.cno 
     WHERE programmerCreditDate >= '11/1/2016' 
      AND programmerCreditDate < '11/29/2016' 
      AND (ClientBilling.programmer='e21' or Clients.admin='e21' or Clients.setup='e21') 
    ) 
    UNION ALL 
    (
     SELECT Clients.aliasname + ' - ' + TradingPartners.aliasname as description 
     FROM RelationshipBilling 
     INNER JOIN [B2BSetups].[dbo].TPRelationships ON TPRelationships.relno = RelationshipBilling.relno 
     INNER JOIN [B2BSetups].[dbo].Clients ON Clients.cno = TPRelationships.cno 
     INNER JOIN [B2BSetups].[dbo].TradingPartners ON TradingPartners.tpno = TPRelationships.tpno 
     WHERE programmerCreditDate >= '11/1/2016' 
      AND programmerCreditDate < '11/29/2016' 
      AND (RelationshipBilling.programmer='e21' or Clients.admin='e21' or Clients.setup='e21') 
    )   
    UNION ALL 
    (
     SELECT Clients.aliasname + ' - ' + TradingPartners.aliasname + ' - ' + RelDocs.document as description 
     FROM DocumentBilling 
     INNER JOIN [B2BSetups].[dbo].RelDocs ON RelDocs.recid = DocumentBilling.docno 
     INNER JOIN [B2BSetups].[dbo].TPRelationships ON TPRelationships.relno = RelDocs.relno 
     INNER JOIN [B2BSetups].[dbo].Clients ON Clients.cno = TPRelationships.cno 
     INNER JOIN [B2BSetups].[dbo].TradingPartners ON TradingPartners.tpno = TPRelationships.tpno 
     WHERE programmerCreditDate >= '11/1/2016' AND programmerCreditDate < '11/29/2016' 
      AND (DocumentBilling.programmer='e21' or Clients.admin='e21' or Clients.setup='e21') 
    ) 
) AS Temp 
ORDER BY description; 

我試圖找回RelationshipBilling.billedAmount,然而,這僅適用於第二/第三選擇查詢作爲頂級關係。內部連接必需的表格需要relno

有沒有辦法檢索這些結果,無論它們是否爲null(因爲有些將是)?

+1

添加具有樣本數據和預期輸出的表結構。 –

回答

1

只需在聯合中的第一個查詢中添加一個虛擬字段,與真實字段的數據類型相同。 cast(null as decimal(9,2)) as billedAmount 然後將billedAmount添加到其他兩個查詢中。

0

您可以將0 as billedAmount置於第二個/第三個查詢的相同順序的第一個查詢中。

1

這是您需要的嗎?

SELECT Clients.aliasname +Temp.description AS description,billedAmount FROM 
    (


     SELECT RelationshipBilling.programmer,TPRelationships.cno , ' - ' + TradingPartners.aliasname as description ,RelationshipBilling.billedAmount 
     FROM RelationshipBilling 
     INNER JOIN [B2BSetups].[dbo].TPRelationships ON TPRelationships.relno = RelationshipBilling.relno 
     INNER JOIN [B2BSetups].[dbo].TradingPartners ON TradingPartners.tpno = TPRelationships.tpno 
     WHERE programmerCreditDate >= '11/1/2016' AND programmerCreditDate < '11/29/2016' 

     UNION ALL 
     SELECT ClientBilling.programmer, ClientBilling.cno,'' AS description,CONVERTNULL AS billedAmount FROM ClientBilling 
     WHERE programmerCreditDate >= '11/1/2016' AND programmerCreditDate < '11/29/2016' 
     UNION ALL 
     SELECT DocumentBilling.programmer,TPRelationships.cno, ' - ' + TradingPartners.aliasname + ' - ' + RelDocs.document as description ,NULL AS billedAmount 
     FROM DocumentBilling 
     INNER JOIN [B2BSetups].[dbo].RelDocs ON RelDocs.recid = DocumentBilling.docno 
     INNER JOIN [B2BSetups].[dbo].TPRelationships ON TPRelationships.relno = RelDocs.relno 
     INNER JOIN [B2BSetups].[dbo].TradingPartners ON TradingPartners.tpno = TPRelationships.tpno 
     WHERE programmerCreditDate >= '11/1/2016' AND programmerCreditDate < '11/29/2016' 


    ) AS Temp 
    INNER JOIN [B2BSetups].[dbo].Clients ON Clients.cno = Temp.cno 
    WHERE 'e21' IN (Clients.setup,Clients.admin,temp.programmer) 
    ORDER BY description;