2015-12-07 77 views
2

我有下面的代碼需要基於參數的條件條件。IS可以實現嗎?如何使用SQL對這種類型。我沒有看到任何varaiation的方法來做到這一點沒有在IN條件的where子句中使用CASE

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 
+0

是否有任何錯誤◆當運行這個? – User2012384

+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') 
) 
+0

這是一個優雅的解決方案 – zedfoxus

+0

不客氣:) –

+0

真棒..謝謝 –