【问题标题】:Use aggregate functions with group by from several tables使用来自多个表的 group by 聚合函数
【发布时间】:2021-09-26 01:45:39
【问题描述】:

我有以下表格允许订阅者通过应用程序销售产品

订单表

OrderId Date
1 2021-07-10
2 2021-08-24

审批表

ApprovalId OrderId Status SellerId
1 1 Accepted 10
2 1 Rejected 20
3 2 Accepted 30

物品表

ItemId OrderId Price Qty SellerId
1 1 620$ 1 10
2 1 150$ 2 10
3 1 410$ 1 20
4 2 220$ 1 30

我想要的是显示> 只接受订单的人

的收入收入
Date Sales Seller_Part 90% Net_Sales 10%
2021-07-10 770$ 693$ 77$
2021-08-24 220$ 198% 22$

我尝试使用 group by 聚合函数,但结果还包括拒绝订单

【问题讨论】:

    标签: mysql database datatables left-join aggregate-functions


    【解决方案1】:
    select o.date
      ,sum(i.price * i.qty) sales 
      ,sum(i.price * i.qty) * 0.90 Seller_Part90% 
      ,sum(i.price * i.qty) * 0.10 Net_Sales10% 
    from Order o
    join Approval a 
      on o.orderId = a.OrderId
      and a.status= 'Accepted'
    join Items i
     on a.sellerid = i.sellerid  
     and a.orderid = i.orderid
    group by o.date
    

    【讨论】:

    • 正确答案,谢谢四位的帮助:)
    猜你喜欢
    • 2017-09-16
    • 2013-12-31
    • 2014-08-24
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多