简体   繁体   English

DB2-聚集带有光标的大小写

[英]DB2 - Aggregate with Case in Cursor

I am trying to create a stored procedure that returns a data set, using a cursor, that contains multiple aggregate functions over subqueries. 我正在尝试创建一个存储过程,该存储过程使用游标返回一个数据集,该数据集包含子查询上的多个聚合函数。 The query works when executed as a standalone script, but when putting it into the stored procedure format using a cursor it doesn't work. 该查询在作为独立脚本执行时有效,但是在使用游标将其置于存储过程格式时不起作用。 When using an aggregate, the code runs fine. 使用聚合时,代码运行良好。 When using an aggregate on a case statement, the stored procedure fails to be created. 在case语句上使用聚合时,将无法创建存储过程。

Input Table data: 输入表数据:

Province |  Contract Date
---------------------------
Ontario  |  June 11th, 2017
Ontario  |  June 21st, 2017
Quebec   |  July 12th, 2017

Query: 查询:

DECLARE C2 CURSOR WITH HOLD WITH RETURN TO CALLER FOR
SELECT 
    count(province) as province_total
    FROM (
       SELECT 
          contract.province,
          contract.contract_date
       WHERE contract.CON_CONTRACT_DATE >='2015-01-01' 
       AND contract.CON_CONTRACT_DATE < '2018-11-01'
    );

Returns: 返回:

Province_Total |  
----------------
3              |  

So this gives me the Province total. 因此,这给了我省的总数。 I am trying to do statistics on how many times a particular province occurs. 我正在尝试统计一个特定省发生了多少次。 I am doing so with the following query: 我这样做与以下查询:

CREATE PROCEDURE test
DYNAMIC RESULT SETS 1  
BEGIN

DECLARE C1 CURSOR WITH RETURN TO CALLER FOR
SELECT 
    count(province) as province_total,
    sum(case province when 'Ontario' then 1.0 else 0.0 end) as ontario_total,
    sum(case province when 'Quebec' then 1.0 else 0.0 end) as quebec_total
    FROM (
       SELECT 
          contract.province,
          contract.contract_date
       FROM dbo.contract as contract
       WHERE contract.CON_CONTRACT_DATE >='2015-01-01' 
       AND contract.CON_CONTRACT_DATE < '2018-11-01'
    );
   OPEN C1;
   END

What I should be getting is: 我应该得到的是:

Province_Total | Ontario_Total | Quebec_Total
----------------------------------------------
3              | 2             | 1

But I'm getting an error on trying to create the procedure. 但是我在尝试创建程序时遇到错误。 Specifically: 特别:

SQL Error [42601]: An unexpected token "END-OF-STATEMENT" was found following "".  Expected tokens may include:  "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.80

From my experience with this error message on DB2, it will be thrown when something is syntactically "wrong". 根据我在DB2上遇到此错误消息的经验,当语法上出现“错误”时,将抛出该错误消息。 The end-of-statement character ';' 语句结尾字符“;” is otherwise recognized. 否则被认可。

Is there any way to obtain my desired result in DB2? 有什么方法可以在DB2中获得所需的结果吗? The use of the cursor in general is required as I need the result set to return 通常需要使用游标,因为我需要返回结果集

Any advice would be greatly appreciated. 任何建议将不胜感激。 Thanks. 谢谢。

Edit: Using DB2 9.5 编辑:使用DB2 9.5

2 Problems. 2个问题。

You must change the default statement separator in your to some new one like in the example. 您必须将示例中的默认语句分隔符更改为一些新的示例分隔符。

FROM clause is missing from your example. 您的示例中缺少FROM子句。

Should be something like this if you run it from DB2 CLP. 如果从DB2 CLP运行它,则应该是这样。

--#SET TERMINATOR @
CREATE PROCEDURE test
DYNAMIC RESULT SETS 1  
BEGIN

DECLARE C1 CURSOR WITH RETURN TO CALLER FOR
SELECT 
    count(province) as province_total,
    sum(case province when 'Ontario' then 1.0 else 0.0 end) as ontario_total,
    sum(case province when 'Quebec' then 1.0 else 0.0 end) as quebec_total
    FROM (
       SELECT 
          contract.province,
          contract.contract_date
    FROM MY_TABLE
       WHERE contract.CON_CONTRACT_DATE >='2015-01-01' 
       AND contract.CON_CONTRACT_DATE < '2018-11-01'
    );
   OPEN C1;
   END@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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