从db2中多个表多个列中获取最大日期的sql查询

x9ybnkn6  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(414)

我有多个具有date_modified列的表,我需要使用所有这些表创建一个视图,但是date_modified应该是这些表的date_modified的最大值。

table1 
    id vendor_number fiscal_year date_created date_modified
    1    124           2021        2021/11/01   2021/11/02
    2    231           2021        2021/11/01   2021/11/03 
    3    232           2021        2021/11/02   NULL
    4    234           2021        2021/11/02   NULL

    table2 
    id fiscal_year discount_amt date_created date_modified
    1  2021          10.30      2021/11/01   2021/11/03
    2  2021          15.23      2021/11/01   2021/11/02
    3  2021          17.45      2021/11/02   2021/11/02
    4  2021          18.49      2021/11/02   NULL

    table3
    id vendor_number date_created date_modified
    1  124           2021/11/01   2021/11/04
    2  231           2021/11/01   2021/11/01
    3  232           2021/11/01   2021/11/03
    4  234           2021/11/02   2021/11/03

    Required Output :

    id|fiscal_year|discount_amt|vendor_number|date_created|date_modified
    1 | 2021      |  10.30     | 124         | 2021/11/01 | 2021/11/04
    2 | 2021      |  15.23     | 231         | 2021/11/01 | 2021/11/03
    3 | 2021      |  17.45     | 232         | 2021/11/02 | 2021/11/03
    4 | 2021      |  18.49     | 234         | 2021/11/02 | 2021/11/03

查看SQL:

CREATE VIEW view_data
AS
  SELECT T1.id, T1.fiscal_year, T2.discount_amt, T3.vendor_number, T1.date_created, max(multiple date_modified columns..from multiple tables..)
    FROM   table1 AS T1
             LEFT JOIN table2 AS T2
                    ON T1.id = T2.id
             LEFT JOIN T3 v
                    ON T1.vendor_number = T3.vendor_number;
z6psavjg

z6psavjg1#

假设id列是每个表的键,您可以连接它们(可能使用完全外部连接),然后使用GREATEST()获取最新日期。
例如:

create view v as 
select
  coalesce(a.id, b.id, c.id) as id,
  b.fiscal_year,
  b.discount_amt,
  a.date_created,
  greatest(a.date_modified, b.date_modified, c.date_modified) as date_modified
from table1 a
full join table2 b on b.id = a.id
full join table3 c on c.id = b.id or c.id = a.id

EDIT:如果您的DB2版本没有实现GREATEST(),您可以用[相当长的] CASE子句替换它:

case when a.date_modified > b.date_modified then
    case when a.date_modified > c.date_modified 
         then a.date_modified else c.date_modified end
  else
    case when b.date_modified > c.date_modified 
         then b.date_modified else c.date_modified end
  end

或者,您也可以自己实现该函数,这非常简单。例如:

create function greatest(in a date, in b date) returns date
language sql
begin
  if a > b then return a; end if;
  return b;
end
//

请参阅db<>fiddle 1中此自定义函数的运行示例。

编辑#2:处理空值

空值不是值,数据库的行为是正确的。如果存在未知日期,引擎将无法确定哪个日期更大,并返回UNKNOWN--以空值的形式。
现在,如果你想让null的行为和C、Java、PHP等编程语言一样,那么你可以1)使用大量的CASE/COALESCE子句,或者你可以简单地修改你的自定义函数,让它按照你喜欢的方式工作。

create function mygreatest(in a date, in b date) returns date
language sql
begin
  if a is null then return b; end if;
  if b is null then return a; end if;
  if a > b then return a; end if;
  return b;
end

然后,您可以看到它的运行情况:

with data (x, y) as (
  select date '2021-07-01', date '2021-12-01' from sysibm.sysdummy1
  union all select date '2021-07-01', null from sysibm.sysdummy1
  union all select null, date '2021-12-01' from sysibm.sysdummy1
  union all select null, null from sysibm.sysdummy1
)
select mygreatest(x, y) as g from data;

结果:

G
----------
2021-12-01
2021-07-01
2021-12-01
null

请参阅db<>fiddle 2上的运行示例。

相关问题