我有以下查询,我需要对其进行修改,以便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 来达到预期的结果吗?