SQL(Interbase):具有聚合函数的字符串的情况

时间:2012-10-16 14:33:17

标签: sql group-by sum case interbase

我无法在Interbase中进行以下SQL工作: 我收到以下错误:

  

第1行出错尝试执行未准备好的动态SQL语句

SQL:

SELECT CASE
   WHEN kl='K' THEN (SUM(DB)-SUM(CR))
   WHEN kl='L' THEN (SUM(CR)-SUM(DB))
   END AS Saldo
FROM  Table1
GROUP BY KL
以这种方式定义的Table1

Column name   Data type
------------  ------------
KL            varchar(1)
DB            int
CR            int

然而,它适用于SQL fiddle

以下也有效!

SELECT CASE 
   WHEN kl='K' THEN 1
   WHEN kl='L' THEN 2
   END as Saldo
FROM  Table1
Group By KL

1 个答案:

答案 0 :(得分:0)

发现它!

SELECT Sum(CASE
       WHEN kl='K' THEN db
       WHEN kl='L' THEN cr
       END )
      -Sum(CASE
       WHEN kl='K' THEN CR
       WHEN kl='L' THEN DB
       END )

 FROM  Table1
Group By KL