Sql 同时从四个表中获取按日期分组的聚合值

Sql 同时从四个表中获取按日期分组的聚合值,sql,count,group-by,teradata,outer-join,Sql,Count,Group By,Teradata,Outer Join,出于一个特定的下游原因,我尝试在一个查询中从四个表中获取聚合数据(Teradata)。我可以通过编写子查询轻松地完成这一点,但不幸的是,我还需要按日期对返回的数据进行分组。每个字段都有一个timestamp属性(事务时间),我希望返回一个包含以下列的表:Date、Count1、Count2、Count3、Count4。理想情况下,每个计数都会有给定日期的交易总数,并且计数会根据表格的不同而有所不同 SELECT (SELECT COUNT(*) FROM TABLE1) AS COUNT1, (

出于一个特定的下游原因,我尝试在一个查询中从四个表中获取聚合数据(Teradata)。我可以通过编写子查询轻松地完成这一点,但不幸的是,我还需要按日期对返回的数据进行分组。每个字段都有一个timestamp属性(事务时间),我希望返回一个包含以下列的表:Date、Count1、Count2、Count3、Count4。理想情况下,每个计数都会有给定日期的交易总数,并且计数会根据表格的不同而有所不同

SELECT (SELECT COUNT(*) FROM TABLE1) AS COUNT1,
(SELECT COUNT(*) FROM TABLE2) AS COUNT2, 
(SELECT COUNT(*) FROM TABLE3) AS COUNT3, 
(SELECT COUNT(*) FROM TABLE4) AS COUNT4,
示例答案:

  • 2015年4月11日52722
  • 2015年4月12日8103

  • 通过这种方式,我可以从所有四个表中获取计数,但我希望选择CAST(Datestamp AS Date)并按此分组,同时获取每个日期的计数。datestamp属性位于每个表中,如何实现这一点?我需要在这里进行多个完全外部联接吗?我觉得加入可能不是必要的,但我想把它写下来!谢谢。

    您可以使用
    完全外部联接
    。您也可以使用
    全部联合
    分组方式
    来执行此操作:

    select dte, max(t1cnt) as t1cnt, max(t2cnt) as t2cnt,
           max(t3cnt) as t3cnt, max(t4cnt) as t4cnt
    from ((select CAST(Datestamp AS Date) as dte, count(*) as t1cnt, 0 as t2cnt, 0 as t3cnt, 0 as t4cnt
           from table1
           group by CAST(Datestamp AS Date)
          ) union all
          (select CAST(Datestamp AS Date) as dte, 0 as t1cnt, count(*) as t2cnt, 0 as t3cnt, 0 as t4cnt
           from table2
           group by CAST(Datestamp AS Date)
          ) union all
          (select CAST(Datestamp AS Date) as dte, 0 as t1cnt, 0 as t2cnt, count(*) as t3cnt, 0 as t4cnt
           from table3
           group by CAST(Datestamp AS Date)
          ) union all
          (select CAST(Datestamp AS Date) as dte, 0 as t1cnt, 0 as t2cnt, 0 as t3cnt, count(*) as t4cnt
           from table4
           group by CAST(Datestamp AS Date)
          ) 
         ) t
    group by dte
    order by dte;
    

    您可以使用
    完全外部联接
    。您也可以使用
    全部联合
    分组方式
    来执行此操作:

    select dte, max(t1cnt) as t1cnt, max(t2cnt) as t2cnt,
           max(t3cnt) as t3cnt, max(t4cnt) as t4cnt
    from ((select CAST(Datestamp AS Date) as dte, count(*) as t1cnt, 0 as t2cnt, 0 as t3cnt, 0 as t4cnt
           from table1
           group by CAST(Datestamp AS Date)
          ) union all
          (select CAST(Datestamp AS Date) as dte, 0 as t1cnt, count(*) as t2cnt, 0 as t3cnt, 0 as t4cnt
           from table2
           group by CAST(Datestamp AS Date)
          ) union all
          (select CAST(Datestamp AS Date) as dte, 0 as t1cnt, 0 as t2cnt, count(*) as t3cnt, 0 as t4cnt
           from table3
           group by CAST(Datestamp AS Date)
          ) union all
          (select CAST(Datestamp AS Date) as dte, 0 as t1cnt, 0 as t2cnt, 0 as t3cnt, count(*) as t4cnt
           from table4
           group by CAST(Datestamp AS Date)
          ) 
         ) t
    group by dte
    order by dte;
    

    下面是一种使用union all组合所有表和条件聚合计算每个(日期、表)组合的行数的方法:

    select 
        myDate,
        count(case when n = 1 then 1 end) count1,
        count(case when n = 2 then 1 end) count2,
        count(case when n = 3 then 1 end) count3,
        count(case when n = 4 then 1 end) count4
    from (
        select 1 n, cast(Datestamp as Date) myDate from table1
        union all select 2, cast(Datestamp as Date) myDate from table2
        union all select 3, cast(Datestamp as Date) myDate from table3
        union all select 4, cast(Datestamp as Date) myDate from table4
    ) t 
    group by myDate
    

    下面是一种使用union all组合所有表和条件聚合计算每个(日期、表)组合的行数的方法:

    select 
        myDate,
        count(case when n = 1 then 1 end) count1,
        count(case when n = 2 then 1 end) count2,
        count(case when n = 3 then 1 end) count3,
        count(case when n = 4 then 1 end) count4
    from (
        select 1 n, cast(Datestamp as Date) myDate from table1
        union all select 2, cast(Datestamp as Date) myDate from table2
        union all select 3, cast(Datestamp as Date) myDate from table3
        union all select 4, cast(Datestamp as Date) myDate from table4
    ) t 
    group by myDate
    

    这种逻辑很有道理,但出于某种原因,它抱怨最后一次联合之后的语法。也许这只是一个teradata的东西,因为它看起来应该可以工作。谢谢,我假设Null和0的最大值将返回零?我这样问是因为我需要零来显示为零,并且在给定的日期/表格中将有很多零。@marchocolate。我修好了。复制和粘贴碎片。这种逻辑非常合理,但出于某种原因,它在最后一次联合之后抱怨语法。也许这只是一个teradata的东西,因为它看起来应该可以工作。谢谢,我假设Null和0的最大值将返回零?我这样问是因为我需要零来显示为零,并且在给定的日期/表格中将有很多零。@marchocolate。我修好了。复制并粘贴碎屑。