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.
Related
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;
I have a few MySQL tables from which I need to JOIN and return data. The return data must show only one row for one of the JOINed tables, but MySQL mixes the rows.
I have tried different methods using subqueries and normal JOIN with GROUP but the results remain pretty much the same.
Example table structure
suppliers
id name ...
1 ACME ...
2 EMCA ...
3 ORG ...`
ratings
id supplier_id rating expiry_date report_file
1 1 5.0 2017-01-31 a.pdf
3 1 7.9 2019-06-30 c.pdf
4 2 5.0 2016-01-31 d.pdf
5 2 2.0 2018-11-30 g.pdf
6 245 9.5 2009-03-31 p.pdf
spends
id report_id supplier_id amount
1 1 1 150.00
2 1 2 100.00
3 1 245 200.00
Here are example queries I have tried to resolve this and return the correct dataset with no luck.
SELECT
reports.id,
suppliers.id AS supplier_id,
suppliers.name,
...
spends.amount,
...
ratings.rating,
ratings.report_file,
ratings.expiry_date
FROM reports
INNER JOIN spends ON reports.id=spends.report_id
INNER JOIN suppliers ON spends.supplier_id=suppliers.id
LEFT JOIN (
SELECT id,
level,
report_file,
supplier_id,
MAX(expiry_date) AS expiry_date
FROM ratings
GROUP BY supplier_id
) ratings ON (ratings.supplier_id=suppliers.id
AND ratings.expiry_date >= reports.period_start)
...
WHERE reports.id = 1
GROUP BY spends.id
ORDER BY spends.amount DESC
Another query
SELECT
reports.id,
suppliers.id AS supplier_id,
suppliers.name,
...
spends.amount,
...
ratings.rating,
ratings.report_file,
MAX(ratings.expiry_date) AS expiry_date
FROM reports
INNER JOIN spends ON reports.id=spends.report_id
INNER JOIN suppliers ON spends.supplier_id=suppliers.id
LEFT JOIN ratings ON (ratings.supplier_id=suppliers.id
AND ratings.expiry_date >= reports.period_start)
...
WHERE reports.id = 1
GROUP BY spends.id
ORDER BY spends.amount DESC
I expect the results to be
id supplier_id name amount rating report_file expiry_date
1 1 ACME 150.00 7.9 c.pdf 2019-06-30
1 2 EMCA 100.00 2.0 g.pf 2018-11-30
1 245 MACE 200.00 null null```
However, the actual output is
```sql
id supplier_id name amount rating report_file expiry_date
1 1 ACME 150.00 5.0 a.pdf 2019-06-30
1 2 EMCA 100.00 5.0 d.pf 2018-11-30
1 245 MACE 200.00 null null
Please could anyone advise how I can fix this.
try a query like this:
SELECT
reports.id,
suppliers.id AS supplier_id,
suppliers.name,
...
spends.amount,
...
r.rating,
r.report_file,
r.expiry_date
FROM reports
INNER JOIN spends ON reports.id=spends.report_id
INNER JOIN suppliers ON spends.supplier_id=suppliers.id
LEFT JOIN ratings r ON r.supplier_id=suppliers.id
AND r.`expire_date` = (
SELECT MAX(`expire_date`) FROM ratings WHERE supplier_id = supplier.id GROUP BY supplier_id)
) as maxdate
...
WHERE reports.id = 1
GROUP BY spends.id
ORDER BY spends.amount DESC
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.
I've got tables:
Table1= USER_ID ITEM_ID
1 12
1 13
2 12
3 12
3 1
3 2
etc..
And second table:
Products = ITEM_ID PRICE
1 1.3
2 0.1
4 22
12 33
13 45
It is just example. How can I get ID's of clients who paid more than average order value?
I tried many times, but I always get errors.
You can do a JOIN between the tables and comparing the average price with specific user price paid like
select t1.user_id
from table1 t1
join products p on t1.item_id = p.item_id
group by t1.user_id
having p.price > avg(p.price);
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