join two columns with same title but in different table - mysql

I have a table pur with values as
CUST_ID AMOUNT
1 100
3 50
1 200
1 500
3 20
and table cus with values as:
CUST_ID
1
2
3
How do I get to show this:
CUST_ID Total_AMOUNT
2 0
3 70
1 800
Simply put, how do I show 2 columns from cus table?

You can use LEFT JOIN and then get SUM of the AMOUNT by grouping rows with the same CUST_ID :
SELECT t2.CUST_ID
, COALESCE(SUM(t1.AMOUNT), 0) AS TOTAL
FROM cus t2 LEFT JOIN pur t1 ON t1.CUST_ID = t2.CUST_ID
GROUP BY t2.CUST_ID
ORDER BY TOTAL
SQLFiddle

Related

name and sum from 2 different tables

I have 2 tables.
table customer have. id , name , age
table order have . id, customer_id , order_amount , order date.
I want to show all name from customer table and sum of order amount from order table according to customer.
customer_id
Name
age
1
Alice
24
2
Bob
52
3
Carol
45
4
Dave
51
order_id
customer_id
order_amount
order_date
1
2
50
2012-4-5
2
1
27
2012-8-1
3
2
12
2013-5-20
4
4
25
2014-1-25
5
4
30
2014-5-30
6
1
20
2014-6-22
EDIT
I tried this but it gives me only bob and sum of all columns instead of separate sum of customers
SELECT customers.name, SUM(orders.order_amount) FROM `orders` INNER JOIN customers WHERE orders.customer_id = customers.customer_id;
Joining condition must be on ON clause, not in WHERE.
You must specify for what group the sum must be calculated.
SELECT customers.name, SUM(orders.order_amount)
FROM `orders`
INNER JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customers.name;

MySQL sums from multiple tables

I have multiple tables and I need to aggregate the data from all of them, but it seems that I always get the wrong results for the sums. What am I doing wrong?
customers
ID Name
1 c1
2 c2
3 c3
budget
ID Cust_ID Value
1 1 100
2 1 300
3 2 600
4 3 450
forecast
ID Cust_ID Value
1 1 200
2 1 500
3 2 100
4 2 700
5 3 550
orders
ID Cust_ID Net_Sales
1 1 100
2 1 200
3 1 300
4 2 400
5 3 500
Here is the expected result:
ID Name sum(budget.Value) sum(forecast.Value) sum(orders.Net_Sales) count(orders.ID)
1 c1 400 700 600 3
2 c2 600 800 400 1
3 c3 450 550 500 1
And here's what I've tried so far:
SELECT customers.ID, customers.Name, sum(budget.Value), sum(forecast.Value), sum(orders.Net_Sales), count(orders.ID)
FROM customers
INNER JOIN budget ON budget.Cust_ID = customers.ID
INNER JOIN forecast ON forecast.Cust_ID = customers.ID
INNER JOIN orders ON orders.Cust_ID = customers.ID
GROUP BY customers.ID
ORDER BY customers.ID ASC
You are joining along multiple dimensions, which multiplies the results.
A simple solution is correlated subqueries:
SELECT c.ID, c.Name,
(SELECT SUM(b.Value)
FROM budget b
WHERE b.Cust_ID = c.ID
) as budget,
(SELECT SUM(f.Value)
FROM forecast f
WHERE f.Cust_ID = c.ID
) as forecast,
(SELECT SUM(o.Net_Sales)
FROM orders o
WHERE o.Cust_ID = c.ID
) as net_sales
FROM customers c
ORDER BY c.ID ASC;
With the right indexes in the second tables (budget(cust_id, value), and so on), this may actually be faster than a JOIN approach.

Select rows of table 1 when sum of colA grouped by ID is bigger than sum of colB in table 2 grouped by ID

We have a table of our sold items, it looks like this : ( Table A )
id
sell_id
item
amount
11
5
A
3000
12
5
B
2000
13
6
A
5120
14
7
C
5000
and a table where shipped items are placed that looks like this : ( Table B )
id
sub_id
item
amount
1
11
A
2850
2
11
A
150
3
12
B
2100
( Table B is matched to Table A by referencing TableA.id in Table B as sub_id ).
I want to find rows that sum of amount per TableA.id is not equivalent of sum of TableB.amount per TableB.sub_id.
In other words I want to know which sold items are not shipped exactly as the amount which is sold.
I've tried left joining tableA to tableB but i cannot get it to work.
Any help would be appreciated. Thanks!
For example:
SELECT a.*
FROM a
WHERE NOT EXISTS ( SELECT NULL
FROM b
WHERE a.id = b.sub_id
GROUP BY b.sub_id
HAVING a.amount = SUM(b.amount) )
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1b13c67b6e622a5da72f63074d53d423

SUM, GROUP BY AND LEFT JOIN in query

I've two tables:
`orders`
order_id order_office_id order_invoice_id
1 1 1
2 2 2
3 2 2
4 2 3
5 1 4
`invoices`
inv_id inv_order_id inv_amount
1 1 500.00
2 0 320.00
3 3 740.00
4 4 160.00
With this query:
SELECT SUM(inv_amount) matrah, order_office_id
FROM `invoices`
LEFT JOIN orders ON order_invoice_id = inv_id OR inv_order_id = order_id
WHERE order_id IS NOT NULL
GROUP BY order_office_id
It is multiplying some amounts.
What I want to get sum of amounts by office:
office_id sum
1 660.00
2 1060.00
How to get proper amounts by single query?
This does what you want, I think
select sum(inv_amount) as matrah,
(select order_office_id from orders where order_invoice_id = inv_id limit 1) as office
from invoices
group by office;
I've removed the OR, because you were getting two office ids for one of the orders, so it was ambiguous. I've included a subquery to make sure that only one office is applied to each order.
This query may help you out:
SELECT SUM(i.inv_amount) matrah, o.order_office_id
FROM `invoices` AS i
LEFT JOIN `orders` AS o
ON o.order_invoice_id = i.inv_id
GROUP BY order_office_id
OR statement in your ON condition caused the problem.

MySQL join, count and group by multiple database

I have this working query
SELECT t1.id as stockid, t1.description as stockdescription, t2.inkoop as price , COUNT(t2.inkoop) as cnt,
(t2.inkoop * COUNT(t2.inkoop)) as totalamount
FROM database1.table_products t1
LEFT JOIN database1.table_stock t2 ON t1.id = t2.stock_id
WHERE 1
GROUP BY t2.inkoop
ORDER BY t1.id ASC
1 database, 2 tables:
t1 is the products 'description' database with ids and description
t2 is the stock, which has a lot of products for what price (purchased) and referenced by stock_id
Output:
id stockdescription price cnt totalamount
1 Product1 1067 15 16005
1 Product1 1290 103 132870
2 Product2 2750 70 192500
3 Product3 500 0 0
But now i have this 2nd database (database2) with a second inventory table (stock2) (exactly the same structure as database1.table_stock)
How do i alter my query so i can also add 'cnt2' and change total to my results?
Like this:
id stockdescription price cnt cnt2 totalcnt totalamount
1 Product1 1067 15 0 15 16005
1 Product1 1290 103 0 103 132870
2 Product2 2750 70 5 75 206250
3 Product3 500 0 4 4 2000
You can join multiple tables, but you'd get the full join of the two stock tables, which means you'd get wrong counts after the GROUP BY. You can avoid that by nesting your queries, e.g. along these lines:
SELECT sub.*, COUNT(stock2.inkoop) AS cnt2
FROM ( <paste your query here> ) AS sub
LEFT JOIN database2.stock2 AS stock2 ON sub.stockid = stock2.stock_id
GROUP BY sub.stockid
ORDER BY sub.stockid ASC
Now you have two left joins, each with its own GROUP BY. So each left join only sees a single left hand table factor, and you won't get duplicates caused by joining too many tables at once.