dtxooq1020 2017-09-05 09:53
浏览 126

比较不同表中两列的SUM()?

I have these two queries:

-- Getting the total no. of items CLAIMED for a sales order
SELECT SUM(qty_claimed) As 'total_items_claimed'
FROM so_claim_item
WHERE sales_order_id = 1;

-- Getting the total no. of items originally ORDERED for a sales order
SELECT SUM(quantity) As 'total_items_ordered'
FROM sales_order_item
WHERE sales_order_id = 1;

-- If the sums of the two columns are equal, return 1 or true. 
-- Else, return 0 or false.

I need to compare the sums of those two columns from different tables to see if a sales order is complete or not.

If 1 is returned, meaning the no. of items claimed is equal to no. of items ordered, it means the sales order is completed. Otherwise, the sales order remains unclaimed/partially claimed.

This is by far the best solution I can think of, but I wish to write it in a single query that will only return one value, if that's possible. But if you have better suggestions, I'm all ears.

  • 写回答

3条回答 默认 最新

  • doumeng4400 2017-09-05 09:58
    关注

    Just using CASE?

    SELECT CASE
           WHEN
           (
               SELECT SUM(qty_claimed) AS 'total_items_claimed'
               FROM so_claim_item
               WHERE sales_order_id = 1
           ) =
           (
               SELECT SUM(quantity) AS 'total_items_ordered'
               FROM sales_order_item
               WHERE sales_order_id = 1
           ) THEN
               1
           ELSE
               0
       END;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法