【问题标题】:SQL- how to join 3 tables but only group by columns from twoSQL-如何连接 3 个表,但只能按两个表中的列分组
【发布时间】:2018-09-03 05:52:46
【问题描述】:

我正在尝试将以下 3 个表连接在一起,但我只想按 customer_ID 分组在前 2 个表中,如何实现?
换句话说,我使用的第三张表只是为了消除记录。非常感谢!

表 1

customer_ID  product_No  
 100          ABC001
 100          ABC111
 100          ABC112 
 200          ABC002

表2

product_No   Amount
 ABC001        10
 ABC002        50
 ABC111        60
 ABC112        70

表 3

Valid_Product  Desc
 ABC001         Y
 ABC111         Y

我可以通过这样做来加入表 1 和 2

select 
    t1.product_No, Max(t2.amount)
from 
    t1, t2
where 
    t1.product_No = t2.product_No
group by 
    customer_ID

现在我如何在同一个查询中加入表 3 并仅获取客户 100 的 ABC111 的值,因为这是具有最大金额的有效产品?

所以结果将是Customer_ID 100 Amount 60 目标是在客户下获得最大数量的产品(仅当产品在第 3 个表中带有 Y 的情况下)。

【问题讨论】:

  • 请描述您要达到的目标。说你想group byjoin 没有帮助。 . .因为那不是你想要做的。您对问题中的查询的尝试完全无效。
  • 表 3 在您想要的输出中的作用是什么?您能说得更具体些吗?
  • Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI JOIN 语法-92 SQL 标准(25 多年前),不鼓励使用它
  • 目前还不清楚:您想要的输出在当前查询的投影中有 CUSTOMER_ID 有 PRODUCT_ID。哪个是正确的?
  • 附注:您应该始终解释您的表格。要么显示创建语句,要么至少告诉我们它们的唯一键。一个产品可以在 t1 中多次出现吗?即使是同一个客户?一个产品可以在 t2 中出现多次吗?一个产品可以在 t3 中出现多次吗?即使使用相同的描述?

标签: sql oracle join group-by max


【解决方案1】:

如果您希望为每个客户提供最大数量的有效产品,请使用row_number()

select *
from (select t1.*, t2.amount,
             row_number() over (partition by t1.customer_id t2.amount desc) as seqnum
      from t1 join
           t2
           on t1.product_No = t2.product_No join
           t3
           on t3.Valid_Product = t1.product_No
     ) t
where seqnum = 1;

【讨论】:

    【解决方案2】:

    对于您的输出示例,简单的聚合函数和连接就足够了

    select t1.*,t2.Amount from table1 t1 join
    
     (
     select * from table2 t2 where amount in
       ( 
        select max(Amount) as amt
       from table2 inner join table3 t3 on 
        t2.product_No=t3.Valid_Product
    
       )  
      ) t2 on t1.product_No=t2.product_No
    

    【讨论】:

      【解决方案3】:

      您想知道某个产品是否存在于表 3 中。我们在 SQL 中使用EXISTSIN 来检查是否存在。

      select 
        t1.customer_id, max(t2.amount)
      from t2
      left join t2 on  t2.product_no = t1.product_no
      where t1.product_no in (select product_no from t3 where desc = 'Y')
      group by t1.customer_id
      order by t1.customer_id;
      

      不要加入,当你只是在查找一个表是否存在条目时。

      【讨论】:

        【解决方案4】:

        这只是另一个连接和过滤t3.desc 的 WHERE 子句。您似乎不需要投影中来自 T3 的列。

        select t1.customer_ID, Max(t2.amount)
        from t1
             join t2
             on t1.product_No = t2.product_No
             join t3
             on t1.product_No = t3.valid_product
        where t3.desc = 'Y'
        Group by t1.customer_ID
        

        顺便说一句,您会注意到我使用 ANSI 92 连接语法编写了查询。 Oracle 自 9i 以来(20 年)就支持这一点,它确实使查询更易于阅读。

        【讨论】:

        • 谢谢。我编辑了这个问题,以更多地解释表 3 中 Y 的目标和用途
        【解决方案5】:

        使用 row_number() 试试下面

        select * from 
        (select customer_ID,t1.product_No, t2.amount, row_number() over (partition by customerid order by amount desc) as rn
        from t1 inner join t2
        where t1.product_No = t2.product_No)a
        inner join table3 on a.product_no = Valid_Product
        where rn=1
        

        【讨论】:

          猜你喜欢
          • 2015-02-04
          • 1970-01-01
          • 2021-07-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多