0

I'm trying to join two tables. Get information of two tables in onw. When I try to run query seperatly on two tables. It gives proper responce. Two tables in consideration are orders and bags.

- Running seperate Query on **Orders **table#

I get the total number of orders total_orders, delivered, number of bags recieved when I run following query in orders table

SELECT customer_id, name_on_delivery, 
COUNT(name_on_delivery) as total_orders, 
SUM(CASE WHEN status='Delivered' THEN 1 END) AS delivered, 
SUM(total_bag_recv_by_driver) as order_bag_recieved, 
FROM `orders` WHERE customer_id = '81992' AND delivered_date > '2023-01-01 00:00:00'

I get following responce which is absolutely right

customer_id   |   name_on_delivery   |   total_orders   |   delivered   |   order_bag_recieved
_______________________________________________________________________________________________
81992         |     Faizan Kamal     |         6        |        6      |            3

- Running seperate Query on **Bags **table#

I get the total number of orders total_orders, delivered, number of bags recieved when I run following query in orders table

SELECT customer_id, 
SUM(received_bag_qty) as collection_bag_recieved
FROM `bags` 
WHERE 
customer_id = '81992' AND 
STATUS='Picked' AND 
pick_date > '2023-01-01'

I get following responce which is absolutely right as well

customer_id   |   collection_bag_recieved   
_____________________________________________
81992         |                20           

Now what I want is to simply show the both record combined and and subrtract the sum of collection_bag_recieved and collection_bag_recieved from the total_orders.

So below is what I want after joining both of above tables

customer_id | name_on_delivery | collection_bag_recieved | total_orders | delivered | order_bag_recieved | bags_with_customer     
_______________________________________________________________________________________________
81992       |     Faizan Kamal  |         20             |       6      |     6     |
    3              |       **-17**        

In above case bags_with_customer should be "-17". To achieve above I'm running following query

SELECT orders.customer_id, 
    SUM(bags.received_bag_qty) as collection_bag_recieved, orders.name_on_delivery, 
    COUNT(orders.name_on_delivery) as total_orders, 
    COUNT(CASE WHEN orders.status='Delivered' THEN 1 END) AS delivered, 
    SUM(orders.total_bag_recv_by_driver) as recieved, 
    (COUNT(CASE WHEN orders.status='Delivered' THEN 1 END) - (SUM(orders.total_bag_recv_by_driver) + SUM(bags.received_bag_qty)) ) as bags_with_customer 
    FROM `orders` 
    JOIN bags ON bags.customer_id = orders.customer_id 
    WHERE 
    bags.customer_id = '81992' AND
    bags.pick_date > '2023-01-01' AND 
    bags.STATUS='Picked' AND 
    orders.delivered_date > '2023-01-01 00:00:00'

Running above query this is what I'm getting . Which is wrong

customer_id | name_on_delivery | collection_bag_recieved | total_orders | delivered | order_bag_recieved | bags_with_customer     
_______________________________________________________________________________________________
81992       |     Faizan Kamal  |         120             |       24      |     24     |        12              |       **-108**        

Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56
  • 1
    Your relationship is clearly 1:n, I suggest you review how inner joins work! You are also not *grouping* in your query, look at [what-are-the-benefits-of-only-full-group-by-mode](https://stackoverflow.com/questions/45484068/what-are-the-benefits-of-only-full-group-by-mode) – Stu Jan 16 '23 at 09:53

0 Answers0