简体   繁体   English

多个左连接的总和

[英]Sum on Multiple Left Joins

I am trying to sum the total value of a column for a specific ID after multiple left joins. 我试图在多次左连接后对特定ID的列的总值求和。

The below code gives me what I am looking for but across multiple rows, I need the value for T3.C_Amt and T4.E_Amt to be totaled. 下面的代码为我提供了我正在寻找的内容,但是在多行中,我需要将T3.C_Amt和T4.E_Amt的值合计。

SELECT
      T1.ID,
      T2.Unique_ID,
      T3.C_Date,
      T3.C_Amount,
      T4.D_Date,
      T4.D_Amount
   FROM 
      TABLE_1 T1
         LEFT JOIN DATABASE1.TABLE_2 T2
            ON T1.ID = T2.UNIQUE_ID
            LEFT JOIN DATABASE1.TABLE_3 T3
               ON T2.Unique_ID = T3.Unique_ID
              AND T3.C_Date = '2019-04-11'
            LEFT JOIN DATABASE1.TABLE_4 T4
               ON T2.Unique_ID = T4.Unique_ID 
              AND T4.D_Date= '2019-04-11'


--this needs to be summed to have the total amount

I want it to return one row for the Unique ID with total C_Amount and total D_Amount for the specific date 我希望它为唯一ID返回一行,其中包含特定日期的总C_Amount和总D_Amount

Use aggregation with group by 使用group by聚合

SELECT T2.Unique_ID,T3.C_Date,sum(T3.C_Amount),
T4.D_Date,sum(T4.D_Amount)
FROM TABLE_1 T1
LEFT JOIN DATABASE1.TABLE_2 T2
ON T1.ID = T2.UNIQUE_ID
LEFT JOIN DATABASE1.TABLE_3 T3
ON T2.Unique_ID = T3.Unique_ID AND T3.C_Date = '2019-04-11'
LEFT JOIN DATABASE1.TABLE_4 T4
ON T2.Unique_ID = T4.Unique_ID AND T4.D_Date= '2019-04-11'
group by T2.Unique_ID,T3.C_Date,T4.D_Date

I would do it this way. 我会这样做的。 Since Teradata is a MPP, there should not be much of a performance impact as well. 由于Teradata是MPP,因此也不会对性能产生太大影响。

    SELECT Unique_ID,C_Date,sum(C_Amount),D_Date,sum(D_Amount)
FROM
(
SELECT
      T1.ID ID,
      T2.Unique_ID Unique_ID,
      T3.C_Date C_Date,
      T3.C_Amount C_Amount,
      T4.D_Date D_Date,
      T4.D_Amount D_Amount
   FROM 
      TABLE_1 T1
         LEFT JOIN DATABASE1.TABLE_2 T2
            ON T1.ID = T2.UNIQUE_ID
            LEFT JOIN DATABASE1.TABLE_3 T3
               ON T2.Unique_ID = T3.Unique_ID
              AND T3.C_Date = '2019-04-11'
            LEFT JOIN DATABASE1.TABLE_4 T4
               ON T2.Unique_ID = T4.Unique_ID 
              AND T4.D_Date= '2019-04-11'
) ABC
GROUP BY Unique_ID,C_Date,D_Date

I would add a concern of 1-to-many causing a false total. 我会添加一对多的问题导致错误的总数。 What if table 3 has 10 records for a given T2.UniqueID and another 5 for the T4 table. 如果表3有给定T2.UniqueID的10条记录,另外5条用于T4表,该怎么办? You have just compounded your total completely out of range. 你刚刚将你的总数完全超出了范围。

As such, I would pre-aggregate from the child tables grouped by the unique ID filtered on the date. 因此,我会从按日期过滤的唯一ID分组的子表中预先聚合。 Also, you can remove the T2 table due to associative properties. 此外,由于关联属性,您可以删除T2表。

T1.ID = T2.Unique_ID = T3.Unique_ID = T4.Unique_ID
to T1.ID = T3.Unique_ID = T4.Unique_ID


SELECT
      T1.ID,
      T3.C_Date,
      T3.C_Amount,
      T4.D_Date,
      T4.D_Amount
   FROM 
      TABLE_1 T1
         LEFT JOIN 
         ( Select Unique_ID, sum( C_Amount ) as T3Sum
              from DATABASE1.TABLE_3
              where T3.C_Date = '2019-04-11'
              group by Unique_ID ) T3
            ON T1.ID = T3.Unique_ID
         LEFT JOIN 
         ( select Unique_ID, sum( D_Amount ) T4Sum                 
              from DATABASE1.TABLE_4 
              where D_Date= '2019-04-11'
              group by Unique_ID ) T4
            ON T1.ID = T4.Unique_ID 

Now, your ambiguity on table names might help being more real-life descriptive. 现在,您对表名的模糊性可能有助于更真实的描述。 Your summary amounts are based on a single date, but how many records in T1 that are applicable? 您的汇总金额是基于单个日期,但T1中有多少记录适用? If you have 5k rows in T1 and only 450 entries total between tables T3 and T4, your result set would still give you all the rows. 如果T1中有5k行,表T3和T4之间只有450个条目,那么结果集仍会为您提供所有行。 That being said, you probably dont want the bloat of records that don't have any such details in the secondary sum subqueries. 话虽这么说,您可能不希望在二级和子查询中没有任何此类详细信息的大量记录。 I would add a WHERE clause at the end 我会在最后添加一个WHERE子句

   WHERE
          T3.Unique_ID IS NOT NULL
      OR  T4.Unique_ID IS NOT NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM