我是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;
我不知道该怎么做请指导我。
答案 0 :(得分:1)
您需要在两个派生表中单独执行计数,然后将它们连接起来。但由于可能存在仅存在于其中一个表中的组合,您必须切换到完全外部加入加上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