db2中复杂的sql查询需求

db2中复杂的sql查询需求,sql,db2,Sql,Db2,我的表格如下: keyCol | date_O_Col | date_C_Col | statCol -------------------------------------------------------------------- 1 2007-02-09 2012-11-02 C 1 1990-01-31 <nu

我的表格如下:

   keyCol     |     date_O_Col      |    date_C_Col   |    statCol
--------------------------------------------------------------------
      1            2007-02-09          2012-11-02          C
      1            1990-01-31          <null>              O
      1            <null>              <null>              O
------------------------------------------------------------------------   
i、 e.将其最初所属列中的最大日期与相应的
statCol
一起发送,并将另一个日期列作为null发送

作为其他示例,表状态为:

       keyCol  |     date_O_Col      |    date_C_Col   |    statCol
--------------------------------------------------------------------
      2            2016-02-09               2016-03-09        C
      2            1990-01-31               2012-11-02        C
      2            2016-03-10               <null>            O
------------------------------------------------------------------------ 
keyCol | date | O | Col | date | C | Col | statCol
--------------------------------------------------------------------
2 2016-02-09 2016-03-09 C
2 1990-01-31 2012-11-02 C
2 2016-03-10 O
------------------------------------------------------------------------ 
应导致:

       keyCol  |     date_O_Col      |    date_C_Col   |    statCol
--------------------------------------------------------------------
      2            2016-03-10               <null>            O
keyCol | date | O | Col | date | C | Col | statCol
--------------------------------------------------------------------
2 2016-03-10 O
因为
2016-03-10
是最长日期,属于
date\u O\u Col
,对应的
statCol
为O


我正在使用DB29。

您可以使用
case

select t.keycol,
       (case when max(date_O_Col) is null and max(date_C_Col) is null then NULL
             when max(date_O_Col) is null then 'C'
             when max(date_C_Col) is null then 'O'
             when max(date_O_Col) > max(date_C_Col) then 'O'
             when max(date_C_Col) > max(date_O_Col) then 'C'
             else '='
        end)
from t
group by keycol;

我为您的问题没有涉及的边界情况编制了值:当值都为
NULL
或两者相等时。

您可以使用
case

select t.keycol,
       (case when max(date_O_Col) is null and max(date_C_Col) is null then NULL
             when max(date_O_Col) is null then 'C'
             when max(date_C_Col) is null then 'O'
             when max(date_O_Col) > max(date_C_Col) then 'O'
             when max(date_C_Col) > max(date_O_Col) then 'C'
             else '='
        end)
from t
group by keycol;

我为您的问题没有涉及的边界情况创建了值:当值都为
NULL
或两者相等时。

您可以使用以下查询:

SELECT keyCol, 
       CASE WHEN date_O_Col = x.date_col THEN date_O_Col END,
       CASE WHEN date_C_Col = x.date_col THEN date_C_Col END,
       statCol
FROM mytable
CROSS JOIN (
   SELECT MAX(date_col) AS date_col
   FROM (SELECT CASE 
                   WHEN COALESCE([date_O_Col], '1900-01-01') > 
                        COALESCE([date_C_Col], '1900-01-01') 
                           THEN date_O_Col 
                   ELSE date_C_Col 
                END AS date_col
         FROM mytable) AS t) AS x
WHERE CASE 
         WHEN COALESCE([date_O_Col], '1900-01-01') > 
              COALESCE([date_C_Col], '1900-01-01') 
                 THEN date_O_Col 
         ELSE date_C_Col 
      END = x.date_col

您可以使用以下查询:

SELECT keyCol, 
       CASE WHEN date_O_Col = x.date_col THEN date_O_Col END,
       CASE WHEN date_C_Col = x.date_col THEN date_C_Col END,
       statCol
FROM mytable
CROSS JOIN (
   SELECT MAX(date_col) AS date_col
   FROM (SELECT CASE 
                   WHEN COALESCE([date_O_Col], '1900-01-01') > 
                        COALESCE([date_C_Col], '1900-01-01') 
                           THEN date_O_Col 
                   ELSE date_C_Col 
                END AS date_col
         FROM mytable) AS t) AS x
WHERE CASE 
         WHEN COALESCE([date_O_Col], '1900-01-01') > 
              COALESCE([date_C_Col], '1900-01-01') 
                 THEN date_O_Col 
         ELSE date_C_Col 
      END = x.date_col

从我的答案到你的另一篇帖子

如果多行具有相同的最长日期,则不能描述所需的结果。简单假设一下,您需要所有的行,那么下面的操作就可以了

select *
from mytable
where (select max(max(coalesce(date1,'00001-01-01')
                      ,coalesce(date2,'00001-01-01') 
                     )
                 )
         from mytable
      ) in (date1, date2)

从我的答案到你的另一个帖子

如果多行具有相同的最长日期,则不能描述所需的结果。简单假设一下,您需要所有的行,那么下面的操作就可以了

select *
from mytable
where (select max(max(coalesce(date1,'00001-01-01')
                      ,coalesce(date2,'00001-01-01') 
                     )
                 )
         from mytable
      ) in (date1, date2)

我不确定我是否完全理解了这些要求,但在Charles提供的查询的引导下,下面的设置和查询可能是:

设置:

create table so39290083 
( keyCol dec(1), date_O_Col date, date_C_Col date,  statCol char)
;
insert into  so39290083 values                                    
/*keyCol dec(1), date_O_Col date, date_C_Col date,  statCol char*/
  (   1   ,       '2007-02-09'  ,     '2012-11-02'    , 'C'  )    
, (   1   ,       '1990-01-31'  ,       null          , 'O'  )    
, (   1   ,        NULL         ,       null          , 'O'  )    
, (   2   ,       '2016-02-09'  ,     '2016-03-09'    , 'C'  )    
, (   2   ,       '1990-01-31'  ,     '2012-11-02'    , 'C'  )    
, (   2   ,       '2016-03-10'  ,       null          , 'O'  )    
;
查询:

select t.keycol                                                  
     ,  case t.statcol when 'O' then date_O_Col end as date_O_Col
     ,  case t.statcol when 'C' then date_C_Col end as date_C_Col
     , t.statcol                                                 
from so39290083 as T                                             
where ( t.keycol                                                 
      , case t.statcol when 'O' then date_O_Col                  
                       when 'C' then date_C_Col end ) in         
      ( select keycol                                            
             , max(max(coalesce( date_O_Col  , date'0001-01-01') 
                      ,coalesce( date_C_Col  , date'0001-01-01') 
                      ))                                         
        from so39290083 s                                        
        group by keycol                                          
      )                                                          
 ; -- likeness of a report from above query:
 KEYCOL  DATE_O_COL  DATE_C_COL  STATCOL
    1    -           2012-11-02     C   
    2    2016-03-10  -              O   
 ********  End of data  ********        

我不确定我是否完全理解了这些要求,但在Charles提供的查询的引导下,下面的设置和查询可能是:

设置:

create table so39290083 
( keyCol dec(1), date_O_Col date, date_C_Col date,  statCol char)
;
insert into  so39290083 values                                    
/*keyCol dec(1), date_O_Col date, date_C_Col date,  statCol char*/
  (   1   ,       '2007-02-09'  ,     '2012-11-02'    , 'C'  )    
, (   1   ,       '1990-01-31'  ,       null          , 'O'  )    
, (   1   ,        NULL         ,       null          , 'O'  )    
, (   2   ,       '2016-02-09'  ,     '2016-03-09'    , 'C'  )    
, (   2   ,       '1990-01-31'  ,     '2012-11-02'    , 'C'  )    
, (   2   ,       '2016-03-10'  ,       null          , 'O'  )    
;
查询:

select t.keycol                                                  
     ,  case t.statcol when 'O' then date_O_Col end as date_O_Col
     ,  case t.statcol when 'C' then date_C_Col end as date_C_Col
     , t.statcol                                                 
from so39290083 as T                                             
where ( t.keycol                                                 
      , case t.statcol when 'O' then date_O_Col                  
                       when 'C' then date_C_Col end ) in         
      ( select keycol                                            
             , max(max(coalesce( date_O_Col  , date'0001-01-01') 
                      ,coalesce( date_C_Col  , date'0001-01-01') 
                      ))                                         
        from so39290083 s                                        
        group by keycol                                          
      )                                                          
 ; -- likeness of a report from above query:
 KEYCOL  DATE_O_COL  DATE_C_COL  STATCOL
    1    -           2012-11-02     C   
    2    2016-03-10  -              O   
 ********  End of data  ********        

你的答案似乎不完整。该查询应产生4列,如我的示例中所述。但上面的答案似乎只返回了两列。另外,如果可能的话,请你解释一下。你的回答似乎不完整。该查询应产生4列,如我的示例中所述。但上面的答案似乎只返回了两列。另外,如果可能的话,请您解释一下。它返回0结果:(@Vicky我已经用第二组样本数据对它进行了测试,它返回了正确的结果。请注意,
[date\u O\u Col]
是无效的(标准)SQL,不能与db2i一起使用它返回0结果:(@Vicky我已经用第二组样本数据对其进行了测试,它返回了正确的结果。请注意,
[date\u O\u Col]
无效(标准)SQL,不适用于db2Year字符串的
组件中有一个额外的零,试图成为日期文字;以下修订将显式键入/强制转换常量作为有效日期:
合并(日期1,日期'0001-01-01')
字符串的
部分中有一个额外的零,试图成为日期文字;以下编校将显式键入/强制转换常量作为有效日期:
合并(日期1,日期'0001-01-01')