在带有IN条件的where子句中使用CASE

时间:2015-12-07 01:38:36

标签: sql sql-server-2008

我有以下代码需要基于参数的案例条件。这可以实现吗?如何使用这种类型的SQL ..我没有看到任何varaiation方法没有案例

BEGIN
DECLARE @INPUT AS VARCHAR(256)
set @input = 'NASS'
SELECT
sum(TRY_PARSE(DV.[ERSDataValues_AttributeValue1] as decimal)) AS NEWVALUE

FROM [VegetablesCoSD-Full].[CoSD].[ERSDataValues] DV LEFT JOIN [CoSD].        [ERSTimeDimension_LU] TD

ON DV.ERSDataValues_ERSTimeDimension_ID = TD.ERSTimeDimension_ID

WHERE DV.ERSDataValues_ERSStatisticType_ID1 = 45 AND 
DV.ERSDataValues_ERSCommodity_ID = 519 AND
DV.ERSDataValues_ERSGeography_ID = 3346 AND
DV.ERSDataValues_ERSUnit_ID1 = 89 AND         

TD.ERSTimeDimension_TimeDimensionType_ID = 11 and 
CASE  
when @INPUT = 'NASS' THEN
TD.ERSTimeDimension_Month in   

('1','2','3','4','5','6','7','8','9','10','11','12')
WHEN @INPUT = 'BLS' THEN
TD.ERSTimeDimension_Month IN('JAN','FEB')
END

1 个答案:

答案 0 :(得分:4)

您可以改为使用OR,如下所示:

AND
(   (@INPUT = 'MASS' AND TD.ERSTimeDimension_Month in ('1','2','3','4','5','6','7','8','9','10','11','12'))
    OR
    (@INPUT = 'BLS' AND TD.ERSTimeDimension_Month IN('JAN','FEB')
)