同时从四个表中获取按日期分组的聚合值
Getting Aggregate Values Grouped By Date from Four Tables Simultaneously
出于特定的下游原因,我试图在一个查询 (Teradata) 中从四个 table 中获取聚合数据。我已经能够通过编写子查询轻松地完成此操作,但不幸的是,我还需要按日期对数据 returned 进行分组。每个字段都有一个时间戳属性(交易时间),我希望 return 一个 table 列:日期、计数 1、计数 2、计数 3、计数 4。理想情况下,每个计数都会包含该天的交易总数,并且计数会因 table.
而异
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 日 5 2 7 22
- 2015 年 4 月 12 日 8 1 0 3
这样我可以从所有四个 table 中获取计数,但我想 SELECT CAST(Datestamp AS Date) 并以此为基础进行分组,同时获取每个日期的计数. datestamp 属性在每个 table 中,我该如何实现?我需要在这里做多个全外连接吗?我觉得加入可能没有必要,但我想把它记下来!谢谢
您可以使用 full outer join
。您也可以使用 union all
和 group by
:
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 组合所有 table 和条件聚合来计算每个 (date,table) 组合的行数的方法:
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) 中从四个 table 中获取聚合数据。我已经能够通过编写子查询轻松地完成此操作,但不幸的是,我还需要按日期对数据 returned 进行分组。每个字段都有一个时间戳属性(交易时间),我希望 return 一个 table 列:日期、计数 1、计数 2、计数 3、计数 4。理想情况下,每个计数都会包含该天的交易总数,并且计数会因 table.
而异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 日 5 2 7 22
- 2015 年 4 月 12 日 8 1 0 3
这样我可以从所有四个 table 中获取计数,但我想 SELECT CAST(Datestamp AS Date) 并以此为基础进行分组,同时获取每个日期的计数. datestamp 属性在每个 table 中,我该如何实现?我需要在这里做多个全外连接吗?我觉得加入可能没有必要,但我想把它记下来!谢谢
您可以使用 full outer join
。您也可以使用 union all
和 group by
:
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 组合所有 table 和条件聚合来计算每个 (date,table) 组合的行数的方法:
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