在具有UNION和JOINS的视图上使用CASE

时间:2015-12-09 15:26:58

标签: sql sql-server case inner-join union

我有一个查询,我已经工作了几天,没有任何教程(w3school,ms等)正在帮助解决这个特殊情况。我无法弄清楚如何使CASE在以下工作:

SELECT T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal,
CASE  T0.CANCELED WHEN 'N' THEN 
(T0.DocTotal - T0.VatSum - T0.TotalExpns) As "Total du document sans TVA",
SUM(T1.LineTotal * (T1.Commission / 100)) As "Total des commissions"
ELSE
((T0.DocTotal - T0.VatSum - T0.TotalExpns) * -1) As "Total du document sans TVA",
(SUM(T1.LineTotal * (T1.Commission / 100)) * -1) As "Total des commissions"
END
FROM OINV T0  
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode 
--WHERE T2.SlpName = N'[%0]' 
--AND T0.DocDate >= '[%1]' 
--AND T0.DocDate <= '[%2]'
GROUP BY T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal, T0.VatSum, T0.TotalExpns, T0.CANCELED

UNION ALL

SELECT T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal * -1, T0.CANCELED,
(T0.DocTotal - T0.VatSum - T0.TotalExpns) * -1 As "Total du document sans TVA",
SUM(T1.LineTotal * (T1.Commission / 100)) * -1 As "Total des commissions"
FROM ORIN T0  
INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode 
--WHERE T2.SlpName = N'[%0]' 
--AND T0.DocDate >= '[%1]' 
--AND T0.DocDate <= '[%2]'
GROUP BY T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal, T0.VatSum, T0.TotalExpns , T0.CANCELED
ORDER BY SlpName, DocNum

我尝试过此查询的多种变体无济于事。感谢您的所有帮助

3 个答案:

答案 0 :(得分:0)

case是一个返回单个值的表达式(好吧,这是多余的;这就是表达式所做的)。所以,使用它两次:

SELECT T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal,
       (CASE WHEN T0.CANCELED = 'N'
             THEN (T0.DocTotal - T0.VatSum - T0.TotalExpns)
             ELSE (SUM(T1.LineTotal * (T1.Commission / 100)) * -1)
        END) As "Total du document sans TVA",
       (CASE WHEN T0.CANCELED = 'N'
             THEN SUM(T1.LineTotal * (T1.Commission / 100))
             ELSE (SUM(T1.LineTotal * (T1.Commission / 100)) * -1)
        END) As "Total des commissions"

答案 1 :(得分:0)

我认为你正在寻找类似的东西:

CASE  T0.CANCELED WHEN 'N' THEN 
(T0.DocTotal - T0.VatSum - T0.TotalExpns)
ELSE
((T0.DocTotal - T0.VatSum - T0.TotalExpns) * -1) 
END As "Total du document sans TVA",
CASE  T0.CANCELED WHEN 'N' THEN 
SUM(T1.LineTotal * (T1.Commission / 100))
ELSE
(SUM(T1.LineTotal * (T1.Commission / 100)) * -1)
END As "Total des commissions"

CASE在过程语言中不像if那样工作,您可以评估条件并根据该比较返回值。

答案 2 :(得分:0)

这样的事情应该有用,虽然我现在无法测试。

SELECT T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal, T0.CANCELED,
CASE  T0.CANCELED WHEN 'N' THEN (T0.DocTotal - T0.VatSum - T0.TotalExpns) ELSE ((T0.DocTotal - T0.VatSum - T0.TotalExpns) * -1) END As "Total du document sans TVA",
CASE  T0.CANCELED WHEN 'N' THEN SUM(T1.LineTotal * (T1.Commission / 100)) ELSE (SUM(T1.LineTotal * (T1.Commission / 100)) * -1) END As "Total des commissions"

FROM OINV T0  
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode 
--WHERE T2.SlpName = N'[%0]' 
--AND T0.DocDate >= '[%1]' 
--AND T0.DocDate <= '[%2]'
GROUP BY T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal, T0.VatSum, T0.TotalExpns, T0.CANCELED

UNION ALL


SELECT T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal * -1 as T0.DocTotal, T0.CANCELED,
(T0.DocTotal - T0.VatSum - T0.TotalExpns) * -1 As "Total du document sans TVA",
SUM(T1.LineTotal * (T1.Commission / 100)) * -1 As "Total des commissions"
FROM ORIN T0  
INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode 
--WHERE T2.SlpName = N'[%0]' 
--AND T0.DocDate >= '[%1]' 
--AND T0.DocDate <= '[%2]'
GROUP BY T2.SlpName, T0.CardName, T0.DocNum, T0.DocType, T0.DocTotal, T0.VatSum, T0.TotalExpns , T0.CANCELED
ORDER BY SlpName, DocNum