【问题标题】:How to find difference between the values of two tables in sql?如何在sql中找到两个表的值之间的差异?
【发布时间】:2021-11-11 19:59:56
【问题描述】:

我有一个表格,其中包含销售和购买产品的供应商的数据。该表包含产品名称、产品价值和交易类型(“购买”或“出售”)。我想写一个查询来了解每个特定产品的损益。 表格示例:

所以我能够找到每个产品对应的值的总和。

select product_name, SUM(product_value) as sell from vendor where transaction_type="sell" group by product_name;
select product_name, SUM(product_value) as buy from vendor where transaction_type="buy" group by product_name;

现在我有了这两个子表,但是如何对这两个表进行差分操作呢?我想要一个结果表,其中包含每个产品的 product_name 和损益值(总销售成本 - 总购买成本)。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    也许您需要一个简单的条件案例来转换您的值,然后您可以简单地减去:

    select productname, 
    Coalesce(Sum(case when TransactionType='sell' then productvalue end),0) -
    Coalesce(Sum(case when TransactionType='buy' then productvalue end),0) ProfitLoss
    from t
    group by productname
    

    Example Fiddle

    【讨论】:

    • 是的,它正在工作,但你能解释一下你到底做了什么吗?
    • 它只是使用case 对每种产品的购买价值和销售价值进行求和,从而为您提供每种产品的总买入和总卖出 - 然后将它们减去从彼此为您的目标值。
    【解决方案2】:

    只是猜测,但也许

    with tb1 as (
      select product_name, SUM(product_value) as sell
      from vendor
      where transaction_type='sell'
      group by product_name
    ),
    tb2 as (
      select product_name, SUM(product_value) as buy
      from vendor
      where transaction_type='buy'
      group by product_name
    )
    select tb1.product_name, tb1.sell, tb2.buy, (tb2.buy - tb1.sell) as profit
    from tb1 left join tb2 on tb1.product_name=tb2.product_name
    

    使用此设置:

    create table vendor (product_name text, product_value int, transaction_type text)
    insert into vendor values ('ABC',3196,'sell'),('XYZ',3196,'sell'),('PQR',3196,'sell'),('ABC',2365,'buy'),('ABC',2365,'buy')
    

    查询返回

    product_name  sell   buy profit
             ABC  3196  4730   1534
             PQR  3196    NA     NA
             XYZ  3196    NA     NA
    

    编辑:这也适用于 MySQL,不知道为什么它不适合你。

    Docker 设置:

    r2@d2sb2:~$ docker pull mysql:latest
    latest: Pulling from library/mysql
    ...
    Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest
    
    r2@d2sb2:~$ docker run --rm -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
    2e93acda94fdb386a9205ccda8df9b4f47b5f6a9bbdd159f84550418dff4a56e
    

    然后在 SQL 中:

    SHOW VARIABLES LIKE 'version';
    # Variable_name  Value
    #       version 8.0.26
    
    create schema stackoverflow;
    use stackoverflow;
    create table vendor (product_name text, product_value int, transaction_type text)
    insert into vendor values ('ABC',3196,'sell'),('XYZ',3196,'sell'),('PQR',3196,'sell'),('ABC',2365,'buy'),('ABC',2365,'buy')
    
    with tb1 as (
      select product_name, SUM(product_value) as sell
      from vendor
      where transaction_type='sell'
      group by product_name
    ),
    tb2 as (
      select product_name, SUM(product_value) as buy
      from vendor
      where transaction_type='buy'
      group by product_name
    )
    select tb1.product_name, tb1.sell, tb2.buy, (tb2.buy - tb1.sell) as profit
    from tb1 left join tb2 on tb1.product_name=tb2.product_name
    # product_name sell  buy profit
    #          ABC 3196 4730   1534
    #          XYZ 3196   NA     NA
    #          PQR 3196   NA     NA
    

    (注意:我在 R DBI 客户端中逐一运行命令,而不是在 mysql 控制台中;我怀疑除了 R 的 NA 作为空值之外它会有所不同。仅供参考。 )

    由于您的实例似乎在没有HAVING 的情况下使用SUM 存在一些问题,因此该查询模式会产生相同的结果:

    with tb1 as (
      select product_name, SUM(product_value) as sell
      from vendor
      group by product_name, transaction_type
      having transaction_type='sell'
    ),
    tb2 as (
      select product_name, SUM(product_value) as buy
      from vendor
      group by product_name, transaction_type
      having transaction_type='buy'
    )
    select tb1.product_name, tb1.sell, tb2.buy, (tb2.buy - tb1.sell) as profit
    from tb1 left join tb2 on tb1.product_name=tb2.product_name
    

    【讨论】:

    • 它给了我misuse of aggregate: SUM() 错误
    • sry,我在不同的 dbms 中进行测试,我没有运行 atm 的 mysql ...也许您需要使用 HAVING 而不是 WHERE
    • 您使用的是哪个 dbms?
    • 这是 sqlite,很高兴你得到了一个可以接受的答案
    • 您的答案也有效,但不适用于 MySQL。
    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2015-03-19
    • 2010-12-25
    • 1970-01-01
    相关资源
    最近更新 更多