简体   繁体   English

在SQL连接操作中,如何从左侧联接中获取行,以及如何从右侧表中获取两列的汇总

[英]In an SQL join operation, how to get the rows from the left join and only the aggregate of two columns from the right table

I am trying to SUM the quantity in tpos table and count the distinct number of stores for each item that is in tpos. 我试图对tpos表中的数量求和,并为tpos中的每个项目计算不同的商店数量。

For each row in inv_dtl there could be mulitple rows in tpos tables. 对于inv_dtl中的每一行,tpos表中可能有多行。 I would like to put a script together that would give me all the rows from the inv_dtl table and add two aggregate columns sum(tpos.quantiy), count(distinct, tpos.store_number) that matches the join condition. 我想将一个脚本放在一起,该脚本将为我提供inv_dtl表中的所有行,并添加两个符合联接条件的聚合列sum(tpos.quantiy),count(distinct,tpos.store_number)。

Here is what I have so far. 这是我到目前为止所拥有的。 The aggregates are working but my output contains the number or rows that match in tpos. 聚合正在工作,但是我的输出包含tpos中匹配的数字或行。

For example 1 row in inv_dtl could have 100 rows in tpos. 例如inv_dtl中的1行在tpos中可能有100行。 My output should contain 1 row plus the two aggregate columns but my current script generates 100 rows. 我的输出应该包含1行加上两个聚合列,但是我当前的脚本会生成100行。

WITH FT1 As
(
  SELECT * FROM inv_dtl WHERE inv_no IN (16084, 23456, 14789)
),
FT2 As
(
  SELECT 
    FT1.*,
    SUM(tpos.quantity) OVER (partition by tpos.item_id) As pos_qty,
    DENSE_RANK() OVER (partition by tpos.store_number ORDER BY tpos.item_id ASC) +
    DENSE_RANK() OVER (partition by tpos.store_number ORDER BY tpos.item_id DESC)
      As unique_store_cnt
    FROM FT1
  LEFT JOIN tpos
    ON tpos.item_id = FT1.ITEM_ID
       And tpos.movement_date Between FT1.SDATE And FT1.EDATE
       And tpos.store_number != 'CMPNY'
)
SELECT * FROM FT2 ORDER BY ITEM_ID

Just use a conventional GROUP BY which will reduce the number of rows. 只需使用常规的GROUP BY,这将减少行数。 But as I have no idea what columns you want from the first mentioned table so I have just invented 4 as an example. 但是由于我不知道要从第一个提到的表中获取哪些列,因此我仅以4个为例。

WITH
      FT1 AS (
                  SELECT
                        col1, col2, col3, col4
                  FROM inv_dtl
                  WHERE inv_no IN (16084, 23456, 14789)
            ),
      FT3 AS (
                  SELECT
                        FT1.col1, FT1.col2, FT1.col3, FT1.col4
                      , SUM(tpos.quantity) OVER (PARTITION BY tpos.item_id) AS pos_qty
                      , ROW_NUMBER() OVER (PARTITION BY col1, col2, col3, col4 ASC) +
                        AS unique_store_cnt
                  FROM FT1
                  LEFT JOIN tpos ON tpos.item_id = FT1.ITEM_ID
                        AND tpos.movement_date BETWEEN FT1.SDATE AND FT1.EDATE
                        AND tpos.store_number != 'CMPNY'
                  GROUP BY
                        FT1.col1, FT1.col2, FT1.col3, FT1.col4
            )
SELECT
      *
FROM FT3
ORDER BY col1, col2, col3, col4

Do pleae note that RANK() and DENSE_RANK() can repeat numbers if data is of "equal rank". 请注意,如果数据属于“相等等级”,则RANK()DENSE_RANK()可以重复数字。 To guarantee a unique integer per row use ROW_NUMBER() instead. 为了保证每行唯一的整数,请使用ROW_NUMBER()

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

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