我有这样的查询:
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
到必要的表。
有没有办法检索这些结果,无论它们是否为空(因为有些结果都是)?
答案 0 :(得分:1)
只需在联合中的第一个查询中添加一个虚拟字段,与真实字段的数据类型相同。cast(null as decimal(9,2)) as billedAmount
然后将billedAmount添加到其他两个查询中。
答案 1 :(得分: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;
答案 2 :(得分:0)
您可以将0 as billedAmount
放在第一个查询中,与第二个/第三个查询的顺序相同。