我有一個查詢,像這樣:如何獲得結果從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(因爲有些將是)?
添加具有樣本數據和預期輸出的表結構。 –