我有以下查询,我需要对其进行修改,以便originDocForCurrency.id = 68
如果没有匹配的commission.referenzid = 68
. 我试过了RIGHT OUTER JOIN
ON commission.referenzid = originDocForCurrency.id
,但如果没有找到记录,它不会给出结果commission.referenzid = 68
。
SELECT
commission.netto AS commissionNetValue,
commission.r_art AS commissionDocClass,
commission.waehrung AS commissionCurrency,
currencies.usd_brief AS foreignCurrencyUSDAsk,
currencies.usd_geld AS foreignCurrencyUSDBid,
originDocForCurrency.r_art AS originDocForCurrencyDocClass,
originDocForCurrency.poswert AS originDocForCurrencyCommission,
(SELECT originDoc.poswert FROM [src_boss_entwicklung].[dbo].[tckopf] AS originDoc WHERE originDoc.id = 68) AS originDocCommission,
(SELECT originDoc.r_art FROM [src_boss_entwicklung].[dbo].[tckopf] AS originDoc WHERE originDoc.id = 68) AS originDocClass,
(SELECT originDoc.waehrung FROM [src_boss_entwicklung].[dbo].[tckopf] AS originDoc WHERE originDoc.id = 68) AS originDocCurrency
FROM
[src_boss_entwicklung].[dbo].[tckopf] AS commission
RIGHT OUTER JOIN
[src_boss_entwicklung].[dbo].[tckopf] AS originDocForCurrency
ON commission.referenzid = originDocForCurrency.id
LEFT OUTER JOIN
[src_boss_entwicklung].[dbo].[devisen] AS currencies
ON currencies.datum = (
SELECT TOP 1 fc.datum
FROM [src_boss_entwicklung].[dbo].[devisen] AS fc
WHERE fc.datum <= originDocForCurrency.von
AND fc.usd_brief IS NOT NULL AND fc.usd_brief > 0
AND fc.usd_geld IS NOT NULL AND fc.usd_geld > 0
ORDER BY fc.datum DESC
)
WHERE
originDocForCurrency.id = 68
AND
( commission.referenzid = 68
AND commission.btyp = 7
AND ( commission.storno <> 1 OR commission.storno IS NULL )
AND ( commission.proforma <> 1 OR commission.proforma IS NULL ) )
OR
( commission.referenzid = 68
AND commission.btyp = 8
AND ( commission.storno <> 1 OR commission.storno IS NULL )
AND ( commission.proforma <> 1 OR commission.proforma IS NULL ) )
如何修改查询以获得所需的结果?我可以CASE
在WHERE
子句中使用 a 来达到预期的结果吗?
为了稍微简化您的查询,您基本上有以下内容:
[src_boss_entwicklung].[dbo].[tckopf]
在列中没有匹配项的地方commission.referenzid
将为 NULL,这意味着这种情况:这些行将不正确(因为
NULL = 68
计算结果为NULL
)。这有效地使您RIGHT OUTER JOIN
的INNER JOIN
我从来都不是
RIGHT JOIN
特别喜欢LEFT JOIN
出现在同一个查询中时。为了连续性,我将重新排列查询以使所有外连接都离开外连接:但这并不能解决问题,因为您仍然有可能
commission.referenzid
将NULL
LEFT OUTER JOIN 变成 INNER JOIN。您可以通过将谓词移动到 JOIN 条件来解决此问题:我也相当肯定这种情况可以简化为:
进行最终查询:
附录
正如评论中指出的那样,您不需要三个子选择,您可以只使用表中的列 aliased
originDocForCurrency
: