无论是否从该供应商处购买,我都需要退回所有供应商。该查询目前仅返回供应商购买的记录。
SELECT vendors.NAME,
Iif([fundingsourceid] = 10, [amount], 0) AS Credit,
Iif(( [fundingsourceid] = 2 )
OR ( [fundingsourceid] = 3 ), [amount], 0) AS EBT,
Iif([fundingsourceid] = 4, [amount], 0) AS [Match],
cardpurchases.updateddate
FROM vendors
FULL OUTER JOIN cardpurchases
ON cardpurchases.vendorid = vendors.vendorid
LEFT JOIN cardfundings
ON cardpurchases.cardfundingid = cardfundings.cardfundingid
INNER JOIN marketevents
ON cardpurchases.marketeventid = marketevents.marketeventid
INNER JOIN markets
ON marketevents.marketid = markets.marketid
WHERE (cardpurchases.updateddate >= '10/22/2014' OR cardpurchases.updateddate IS NULL)
AND (cardpurchases.updateddate < '10/23/2014' OR cardpurchases.updateddate IS NULL)
AND (markets.marketid = 47 OR markets.marketid IS NULL)
ORDER BY vendors.NAME
答案 0 :(得分:1)
虽然您在查询中稍后指定了FULL OUTER JOIN,但您根据cardpurchases表中的列限制结果集,这会导致没有卡购买的供应商消失。
您可以执行以下任一操作:
WHERE
((cardpurchases.updateddate >= '10/22/2014'
AND cardpurchases.updateddate < '10/23/2014')
OR cardpurchases.updateddate IS NULL)
AND markets.marketid = 47
或者
FROM vendors
LEFT JOIN cardpurchases
ON cardpurchases.vendorid = vendors.vendorid
AND cardpurchases.updateddate >= '10/22/2014'
AND cardpurchases.updateddate < '10/23/2014')
答案 1 :(得分:0)
您需要在NULL
子句中考虑WHERE
:
WHERE (cardpurchases.updateddate >= '10/22/2014' OR cardpurchases.updateddate IS NULL)
AND (cardpurchases.updateddate < '10/23/2014' OR cardpurchases.updateddate IS NULL)
AND (markets.marketid = 47 OR markets.marketid IS NULL)
您还应该使用括号来控制联接,以便后来的INNER JOIN
不会使其中毒:
FROM vendors
FULL OUTER JOIN (cardpurchases
LEFT JOIN cardfundings
ON cardpurchases.cardfundingid = cardfundings.cardfundingid
INNER JOIN marketevents
ON cardpurchases.marketeventid = marketevents.marketeventid
INNER JOIN markets
ON marketevents.marketid = markets.marketid)
ON cardpurchases.vendorid = vendors.vendorid
这只是部分的,因为您LEFT JOIN
会遇到与FULL
相同的问题。