带有NULL SQL错误的DB2组[22007]

时间:2016-09-22 10:17:36

标签: sql db2 ibm-midrange

我有数据格式查询。我尝试将其转换为整数,但它对我没有帮助,我不能分组,因为我在行中有NULL。

SELECT t."Reception Date", CASE WHEN t."DT" IS NULL THEN 100
                                ELSE 100500
                           END AS "GG" 
FROM fit t
GROUP BY t."Reception Date", CASE WHEN t."DT" IS NULL THEN 100
                                  ELSE 100500
                             END

我有很多行,所以我想对它进行分组,但出现错误

  

SQL错误[22007]:[SQL0181]日期,时间或时间戳字符串中的值   无效。 java.sql.SQLException:[SQL0181]日期,时间或值的值   时间戳字符串无效。

Reception Date  GG
20160921        100500
20160921        0

示例数据:

create table qtemp/fit
(
    "Reception Date" char(10),
    "DT" int
);

insert into qtemp/fit values
('20160921', 140816),
('20160921', 250816),
('20160921', 180816),
('20160921', 70916),
('20160921', 10916),
('20160921', 210916),
('20160921', 210916),
('20160921', 210916),
('20160921', 200916),
('20160921', 0),
('20160921', 0),
('20160921', 0),
('20160921', 0),
('20160921', 0),
('20160921', 210916)

1 个答案:

答案 0 :(得分:1)

带样品日期(假设):

create table qtemp/tstdta 
(
    "Reception Date" char(10),
    "DT" int
);

insert into qtemp/tstdta values
('20160921', 140816),
('20160921', 250816),
('20160921', 180816),
('20160921', 70916),
('20160921', 10916),
('20160921', 210916),
('20160921', 210916),
('20160921', 210916),
('20160921', 200916),
('20160921', 0),
('20160921', 0),
('20160921', 0),
('20160921', 0),
('20160921', 0),
('20160921', null),
('20160921', null),
('20160921', null)

<强>解决方案:

WITH as t (
    SELECT "Reception Date",
          CASE WHEN "DT" IS NULL THEN 100
               ELSE 100500
               END AS "GG"
     FROM qtemp/tstdta
) 
SELECT "Reception Date", "GG", count(*) as COUNT
FROM t
GROUP BY "Reception Date", "GG"

<强>结果:

 Reception Date  GG           COUNT        
 ------------------------------------------
 20160921        100          3             
 20160921        100500       14