[英]Teradata Substract Columns of two tables in one query
我是Teradata的新手,我有两个表,假设tbl_A和tbl_B具有相同的架构。 我的目标是:
架构为
Library_Card_Type Student_Class Book_Type
c1 A NOVEL
c2 B HISTORY
c3 C MOVIE
c4 D GK
c1 E POETRY
c1 A NOVEL
c2 B HEALTH
c3 C POLITICS
c4 D SQL
c1 E JAVA
当我按Library_Card_Type,Student_Class和Book_Type对数据分组时,我想得到tbl_A和tbl_b的count(*)的差,我尝试过的查询是:
sel count(*) AS Count_of_tbl_A,Library_Card_Type AS Library_Card_Type ,Book_Type AS Book_Type,Student_Class AS Student_Class group by 2,3,4 order by 2,3,4 from tbl_A ;
Union all
sel count(*) AS Count_of_tbl_B,Library_Card_Type AS Library_Card_Type ,Book_Type AS Book_Type,Student_Class AS Student_Class group by 2,3,4 order by 2,3,4 from tbl_B;
sel (Count_of_tbl_A - Count_of_tbl_B) As Overall_Difference;
其他方法是
Sel Count_of_tbl_B from (sel count(*) AS Count_of_tbl_B,Library_Card_Type AS Library_Card_Type ,Book_Type AS Book_Type,Student_Class AS Student_Class group by 2,3,4 order by 2,3,4 from tbl_B) A,
Sel Count_of_tbl_A from (sel count(*) AS Count_of_tbl_A,Library_Card_Type AS Library_Card_Type ,Book_Type AS Book_Type,Student_Class AS Student_Class group by 2,3,4 order by 2,3,4 from tbl_A) B,
(Count_of_tbl_A - Count_of_tbl_B) as minus_result sample 10;
and the inner join also
select
(sel count(*) AS Count_of_tbl_A,Library_Card_Type AS Library_Card_Type ,Book_Type AS Book_Type,Student_Class AS Student_Class group by 2,3,4 order by 2,3,4 from tbl_A) AS Count_of_tbl_A
, (sel count(*) AS Count_of_tbl_A,Library_Card_Type AS Library_Card_Type ,Book_Type AS Book_Type,Student_Class AS Student_Class group by 2,3,4 order by 2,3,4 from tbl_B) AS Count_of_tbl_B
,A.Library_Card_Type AS Library_Card_Type1
,B.Library_Card_Type AS Library_Card_Type
,A.Book_Type AS Book_Type1
,B.Book_Type AS Book_Type
,A.Student_Class AS Student_Class1
,B.Student_Class AS Student_Class
,(Count_of_tbl_A - Count_of_tbl_B ) AS Difference_of_Count
FROM tbl_B B
INNER JOIN
tbl_A A
ON A.Library_Card_Type=B.Library_Card_Type AND
A.Book_Type=B.Book_Type AND
A.Student_Class=B.Student_Class;
我不知道该怎么做,请指导我。
您需要在两个派生表中分别进行计数,然后将它们合并。 但是由于可能只有一个表存在组合,因此您必须切换到完全外部联接加COALESCE:
select
coalesce(A.Library_Card_Type, B.Library_Card_Type) as Library_Card_Type
,coalesce(A.Book_Type, B.Book_Type) as Book_Type
,coalesce(A.Student_Class, B.Student_Class) as Student_Class
,coalesce(A.cnt,0) - coalesce(B.cnt, 0) as difference
,coalesce(A.cnt,0) as tblA_cnt
,coalesce(B.cnt,0) as tblB_cnt
from
(
select
Library_Card_Type
,Book_Type
,Student_Class
,count(*) AS cnt
from tbl_A
group by 1,2,3
) as A
full join
(
select
Library_Card_Type
,Book_Type
,Student_Class
,-count(*) AS cnt
from tbl_B
group by 1,2,3
) as B
ON A.Library_Card_Type=B.Library_Card_Type
AND A.Book_Type=B.Book_Type
AND A.Student_Class=B.Student_Class
或者,您切换到另一种方法,UNION既可以得到结果,也可以进行汇总:
select
Library_Card_Type
,Book_Type
,Student_Class
,sum(cnt) as difference
,max(cnt) as tblA_count
,abs(min(cnt)) as tblB_count
from
(
select
Library_Card_Type
,Book_Type
,Student_Class
,count(*) AS cnt -- positive numbers
from tbl_A
group by 1,2,3
union ALL -- more efficient than UNION
select
Library_Card_Type
,Book_Type
,Student_Class
,- count(*) AS cnt -- negative numbers
from tbl_B
group by 1,2,3
) as dt
group by 1,2,3
在这两种情况下,您都可以添加过滤器以仅显示差异,
where difference <> 0 -- join query
要么
having difference <> 0 -- union query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.