coldfusion计数案例错误取决于数据源

时间:2014-10-21 12:53:21

标签: sql-server coldfusion count ms-access-2007 case

我正在使用coldfusion使用COUNT CASE语句从数据库中检索某些值。

下面的SQL可以很好地对抗SQL服务器数据源:

<cfquery name="getData" datasource="cfTSP1_Dev">
  SELECT  SiteFK,
      Scheme,
      COUNT(case when DrawingTypeFK = 'Civil' then 1 else null end) as CCount,
      COUNT(case when DrawingTypeFK = 'Mechanical' then 1 else null end) as MCount,
      COUNT(case when DrawingTypeFK = 'Electrical' then 1 else null end) as ECount,
      COUNT(case when DrawingTypeFK = 'P & ID' then 1 else null end) as PCount,
      COUNT(case when DrawingTypeFK = 'Zoning' then 1 else null end) as ZCount
  FROM     Drawings.tblDrawingsData
  where     SiteFK = 'MYSITE'
  group by  SiteFK,Scheme
  order by Scheme

但是当我将查询更改为指向访问数据库中完全相同的表时,我收到错误:

<cfquery name="getData" datasource="drawingsLink">
  SELECT  SiteFK,
      Scheme,
      COUNT(case when DrawingTypeFK = 'Civil' then 1 else null end) as CCount,
      COUNT(case when DrawingTypeFK = 'Mechanical' then 1 else null end) as MCount,
      COUNT(case when DrawingTypeFK = 'Electrical' then 1 else null end) as ECount,
      COUNT(case when DrawingTypeFK = 'P & ID' then 1 else null end) as PCount,
      COUNT(case when DrawingTypeFK = 'Zoning' then 1 else null end) as ZCount
  FROM     tblDrawingsData
  where     SiteFK = 'MYSITE'
  group by  SiteFK,Scheme
  order by Scheme

错误是这样的:

[Macromedia] [SequeLink JDBC驱动程序] [ODBC套接字] [Microsoft] [ODBC Microsoft Access驱动程序]查询表达式'COUNT中的语法错误(缺少运算符)(DrawingTypeFK ='Civil'然后1其他为空结束时的情况)' 。

任何想法为什么?我唯一的理论是它的访问驱动程序,但我使用这个数据源的其他查询(没有Count Case)都没问题。

干杯 尼尔

2 个答案:

答案 0 :(得分:1)

访问&#39; db引擎不支持CASE ... WHEN,因此您需要重写查询。您可以在Access SQL中使用IIf()来近似CASE ... WHEN

SELECT  SiteFK,
  Scheme,
  Count(IIf(DrawingTypeFK = 'Civil', 1, null)) as CCount

Count()仅计算非空值,因此应该为您提供我想要的内容。但是,如果您想按照评论中的建议使用Sum(),那么这也应该有效:

Sum(IIf(DrawingTypeFK = 'Civil', 1, 0)) as CCount

答案 1 :(得分:0)

我还没有测试过这段代码,我想它的代码效率会低得多,但它应该可以在MS Access中运行。如果您的代码在您需要的任何地方工作,我会说它与下面的代码相符。

SELECT
        SiteFK,
        Scheme,
        (
            SELECT  count(*)
            FROM    Drawings.tblDrawingsData Civil
            WHERE   Civil.Scheme = tblDrawingsData.Scheme
                AND Civil.DrawingTypeFK = 'Civil'
        ) AS CCount,
        (
            SELECT  count(*)
            FROM    Drawings.tblDrawingsData Civil
            WHERE   Civil.Scheme = tblDrawingsData.Scheme
                AND Civil.DrawingTypeFK = 'Mechanical'
        ) AS MCount,
        (
            SELECT  count(*)
            FROM    Drawings.tblDrawingsData Civil
            WHERE   Civil.Scheme = tblDrawingsData.Scheme
                AND Civil.DrawingTypeFK = 'Electrical'
        ) AS ECount,
        (
            SELECT  count(*)
            FROM    Drawings.tblDrawingsData Civil
            WHERE   Civil.Scheme = tblDrawingsData.Scheme
                AND Civil.DrawingTypeFK = 'P & ID'
        ) AS PCount,
        (
            SELECT  count(*)
            FROM    Drawings.tblDrawingsData Civil
            WHERE   Civil.Scheme = tblDrawingsData.Scheme
                AND Civil.DrawingTypeFK = 'Zoning'
        ) AS ZCount
  FROM      Drawings.tblDrawingsData
  WHERE     SiteFK = 'MYSITE'
  GROUP BY  SiteFK, Scheme
  ORDER BY  Scheme

这是一个相关的子查询,所以它可能非常慢。买家要小心。