Teradata 在一个查询中减去两个表的列

Teradata Substract Columns of two tables in one query

我是 teradata 的新手 我有两个表假设 tbl_A 和 tbl_B 具有相同的架构。 我的Objective是:

  1. 当我对第 1、2、3 列的数据进行分组时,得到两个表的计数 (*) 的差异

架构如

   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 和 [=35 对数据进行分组时,我想得到 tbl_A 和 tbl_b 的计数 (*) =] 我试过的查询是:

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;

我不知道怎么做,请指导我。

您需要分别在两个派生表中进行计数,然后将它们合并。但是由于可能存在仅存在于其中一个 table 上的组合,因此您必须切换到 Full Outer Join 加 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