2012-11-05 58 views
1

我已經查看了其他類似的問題,但我在查詢中沒有Where子句,這讓我有點困惑至於如何解決它。在預期條件的上下文中指定的非布爾類型表達式,在','附近

INSERT INTO #tmpCombined 
SELECT A.Region 
    , 'MyLocation' AS 'Location' 
    , c.Id AS 'CompID' 
    , c.Name AS 'CompName' 
    , 'Cash' AS 'Context' 
    , isnull(sum(CASE WHEN T.TranAge >= 0 and CASE WHEN T.TranAge < 1 THEN 1 ELSE 0 END,0)) AS 'ZeroDays0' 
    , isnull(sum(CASE WHEN T.TranAge >= 1 and CASE WHEN T.TranAge < 8 THEN 1 ELSE 0 END), 0) AS '1to7Days' 
    , isnull(sum(CASE WHEN T.TranAge >= 8 and CASE WHEN T.TranAge < 16 THEN 1 ELSE 0 END), 0) AS '8to15Days' 
    , isnull(sum(CASE WHEN T.TranAge >= 16 and CASE WHEN T.TranAge < 20 THEN 1 ELSE 0 END), 0) AS '16to20Days' 
    , isnull(sum(CASE WHEN T.TranAge >= 20 THEN 1 ELSE 0 END), 0) AS '20DaysPlus' 
    , COUNT(1) AS 'Total' 
    , A.Level as 'Level' 
    , sum(T.USDDifference) AS 'USDRevaluation' 
    from MyLocation.dbo.Company C 
    inner join #tmpCashTBResults T on T.CompanyId = C.Id 
    inner join #tmpCashAccounts A on T.CompanyId = A.CompanyId 
    group by A.Region, C.Id, C.Name, A.Level 

回答

0

相近的查詢

  1. 修復使用單引號的別名已被棄用。習慣於使用方括號,或者在別名正常時留下它們
  2. 當您已經使用SUM(CASE.. WHEN.. THEN 1 ELSE 0 END)時,CASE語句的性質是它永遠不是NULL,所以用ISNULL包裝是多餘的。
  3. CASE的基本結構是CASE WHEN <condition> THEN <value>。這裏的<condition>可以是任何表達式,甚至可以是多個比較。

修正並重新格式化查詢:

INSERT INTO #tmpCombined 
    SELECT A.Region 
     , 'MyLocation' AS Location 
     , c.Id AS CompID 
     , c.Name AS CompName 
     , 'Cash' AS Context 
     , sum(CASE WHEN T.TranAge >= 0 and T.TranAge < 1 THEN 1 ELSE 0 END) AS ZeroDays0 
     , sum(CASE WHEN T.TranAge >= 1 and T.TranAge < 8 THEN 1 ELSE 0 END) AS [1to7Days] 
     , sum(CASE WHEN T.TranAge >= 8 and T.TranAge < 16 THEN 1 ELSE 0 END) AS [8to15Days] 
     , sum(CASE WHEN T.TranAge >= 16 and T.TranAge < 20 THEN 1 ELSE 0 END) AS [16to20Days] 
     , sum(CASE WHEN T.TranAge >= 20 THEN 1 ELSE 0 END) AS [20DaysPlus] 
     , COUNT(1) AS Total 
     , A.Level as Level 
     , sum(T.USDDifference) AS USDRevaluation 
    from MyLocation.dbo.Company C 
    join #tmpCashTBResults T on T.CompanyId = C.Id 
    join #tmpCashAccounts A on T.CompanyId = A.CompanyId 
group by A.Region, C.Id, C.Name, A.Level; 
0

該位的位置:

THEN 1 ELSE 0 END,0)) AS 'ZeroDays0' 

應該

THEN 1 ELSE 0 END) ,0) AS 'ZeroDays0' 

吧?

1

CASE WHEN T.TranAge> = 0和CASE WHEN T.TranAge < 1 THEN 1

這應該是

CASE 
    WHEN T.TranAge >= 0 and T.TranAge < 1 THEN 1 
... 

這是用於指定使用CASE化合物的條件的語法...當構建。

+1

說,除了第一'isnull'行語法錯誤做的'不正確的位置)'。 – Oded

+0

謝謝Vikdor,我應該認識到這一點!感謝Oded也。 – user1773949

0

你在錯誤的地方,0這裏,改變這種:

isnull(sum(CASE WHEN T.TranAge >= 0 and CASE WHEN T.TranAge < 1 THEN 1 ELSE 0 END,0)) 

到:

isnull(sum(CASE WHEN T.TranAge >= 0 and CASE WHEN T.TranAge < 1 THEN 1 ELSE 0 END), 0) 

而且,你case陳述是錯誤的,不應該有另一個case在條件。改變看起來像這樣的人;

CASE WHEN T.TranAge >= 0 and CASE WHEN T.TranAge < 1 THEN 1 ELSE 0 END 

到:

CASE WHEN T.TranAge >= 0 and T.TranAge < 1 THEN 1 ELSE 0 END 
0

請在您的案件條件的變化:

, isnull(sum(CASE WHEN T.TranAge >= 0 and T.TranAge < 1 THEN 1 ELSE 0 END),0) AS 'ZeroDays0' 
0

爲您傳遞,0作爲SUM函數的參數ZeroDays0列,而不是ISNULL。

相關問題