简体   繁体   中英

An expression of non-boolean type specified in a context where a condition is expected, near ','

I've reviewed other similar questions on this however I do not have a Where clause in my query which leaves me a little confused as to how to resolve it.

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  

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

This should be

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

This is the syntax for specifying compound conditions using CASE ... WHEN construct.

This bit here:

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

should be

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

perhaps?

You the ,0 in the wrong place here, change this:

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

to:

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

Also, your case statements are wrong, there shouldn't be another case in the condition. Change those looking like this;

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

to:

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

请更改您的案情:

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

对于ZeroDays0列,您将传递,0作为SUM函数的参数,而不是ISNULL。

Things to fix in your query

  1. Using single-quoted alias names is deprecated. Get used to using square brackets, or leave them when the aliases are fine
  2. When you are already using SUM(CASE.. WHEN.. THEN 1 ELSE 0 END) , the nature of the CASE statement is that it is never NULL, so wrapping that with an ISNULL is redundant.
  3. The basic structure of CASE is CASE WHEN <condition> THEN <value> . The <condition> here can be any expression, even multiple comparisons.

The corrected and reformatted query:

  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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM