2017-10-06 63 views
1

我有以下查詢返回一個列表:SQL服務器的選擇的情況下在聲明中

SELECT cpa_0.[Period] as "Period", cpa_0.[Account] as "Account", cpa_0.[Center] as "Center", cpa_0.[Scenario] as "Scenario", cpa_0.[Effective_Date] as "Effective_Date" 
    FROM [dbo].[GLDetail] cpa_0 
    WHERE cpa_0.[Center] = $$Centers-VALUE$$ and 
      cpa_0.[Account] = $$Accounts-VALUE$$ and 
      cpa_0.[Period] = case 
      when $$View-VALUE$$ = 'Periodic' then $$Periods-VALUE$$ 
      when $$View-VALUE$$ = 'Y_T_D' then ('2017-09','2017-08') 
      end 

的價值$$是變量過去了,他們工作得很好。我的問題是查詢的最後一行,當$$ View-VALUE $$ ='Y_T_D'then('2017-09','2017-08')時,這不起作用。當我的變量等於Y_T_D時,我需要返回一個列表,我該怎麼做?

Regards

+0

旁註:你試圖在這裏做可能不執行,以及你所期望的。看到[這個問題](https://stackoverflow.com/questions/26919158/performance-considerations-for-conditional-queries-search-forms)和博客文章鏈接在頂部的答案。 –

回答

2

A WHERE子句是一個布爾表達式。這裏不需要額外創建布爾表達式CASE WHEN;只需使用ANDOR即可。

SELECT 
    Period, 
    Account 
    Center, 
    Scenario, 
    Effective_Date 
FROM dbo.GLDetail 
WHERE Center = $$Centers-VALUE$$ 
    AND Account = $$Accounts-VALUE$$ 
    AND 
    (
    ($$View-VALUE$$ = 'Periodic' AND Period = $$Periods-VALUE$$) 
    OR 
    ($$View-VALUE$$ = 'Y_T_D' AND Period IN ('2017-09', '2017-08')) 
); 
0

您可以更改where子句是這樣的:

SELECT cpa_0.[Period] as "Period", cpa_0.[Account] as "Account", cpa_0.[Center] as "Center", cpa_0.[Scenario] as "Scenario", cpa_0.[Effective_Date] as "Effective_Date" 
FROM [dbo].[GLDetail] cpa_0 
WHERE cpa_0.[Center] = $$Centers-VALUE$$ and 
     cpa_0.[Account] = $$Accounts-VALUE$$ and 
     case 
     when $$View-VALUE$$ = 'Periodic' AND cpa_0.[Period] = $$Periods-VALUE$$ THEN 1 
     when $$View-VALUE$$ = 'Y_T_D' AND cpa_0.[Period] IN ('2017-09','2017-08') THEN 1 
     ELSE 0 
     END = 1 
+0

男士們非常感謝。我需要解析$$ periods.value $$,以便在ytd時,我需要返回選定之前的所有時間段。 – user1766419