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  
4

6 回答 6

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 ... WHEN 构造指定复合条件的语法。

于 2012-11-05T09:54:04.370 回答
0

这一点在这里:

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

应该

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

也许?

于 2012-11-05T09:53:48.740 回答
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
于 2012-11-05T09:54:05.617 回答
0

请更改您的案例条件:

, isnull( sum( CASE WHEN T.TranAge >= 0 and T.TranAge < 1 THEN 1 ELSE 0 END ),0) AS 'ZeroDays0'  
于 2012-11-05T09:54:46.550 回答
0

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

于 2012-11-05T09:54:51.793 回答
0

查询中要修复的问题

  1. 不推荐使用单引号别名。习惯使用方括号,或者当别名很好时离开它们
  2. 当您已经在使用SUM(CASE.. WHEN.. THEN 1 ELSE 0 END)时,CASE 语句的性质是它永远不会为 NULL,因此用 ISNULL 包装它是多余的。
  3. 的基本结构CASECASE 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;
于 2012-11-05T10:17:30.520 回答