Good day everyone I would like a query that can give me both maximum and minimum sum values. Specifically i have the following tables:
PRODUCT
_____________________________
productID | categoryID| name|
__________|___________|_____|
1 1 "name1"
2 1 "name2"
3 1 "name3"
4 2 "name4"
5 2 "name5"
6 1 "name6"
7 2 "name7"
8 2 "name8"
AND:
PURCHASES
_____________________________
purchaseID | productID| quantity|
___________|___________|_________|
1 1 12
2 2 13
3 4 55
4 4 66
5 5 99
6 6 99
7 5 88
8 7 12
so basically i have to show the product that was bought the most and the product that was bought the least.. T have tried this:
SELECT pr.name, max(sum(p2.quantity))
FROM purchase as p2, product as pr
WHERE p2.IDproduct=pr.IDproduct
Group by p2.IDproduct desc
but I get Error Code 1111: Invalid use of group function.
For max Product
select t.name,max(t.sum1) MaxProduct
FROM
(SELECT a.name, sum(b.quantity) sum1
FROM PRODUCT a
INNER JOIN PURCHASES b
ON a.productID = b.productID
GROUP BY a.productID )t
group by t.name order by MaxProduct desc limit 1
FOR COMBINE RESULT
(select t.name,max(t.sum1) MaxProduct
FROM
(SELECT a.name, sum(b.quantity) sum1
FROM PRODUCT a
INNER JOIN PURCHASES b
ON a.productID = b.productID
GROUP BY a.productID )t
group by t.name order by MaxProduct desc limit 1)
UNION ALL
(select t1.name,min(t1.sum1) MaxProduct
FROM
(SELECT a.name, sum(b.quantity) sum1
FROM PRODUCT a
INNER JOIN PURCHASES b
ON a.productID = b.productID
GROUP BY a.productID )t1
group by t1.name order by MaxProduct asc limit 1)
SQL FIDDLE
Hacky, but it works
(
SELECT
SUM(pur.quantity) quant,
prod.name name
FROM Purchases pur
INNER JOIN Products prod
ON prod.productID = pur.productID
GROUP BY pur.productID
ORDER BY quant DESC
LIMIT 1
)
UNION
(
SELECT
SUM(pur.quantity) quant,
prod.name name
FROM Purchases pur
INNER JOIN Products prod
ON prod.productID = pur.productID
GROUP BY pur.productID
ORDER BY quant ASC
LIMIT 1
)
SQLFiddle
As I received a Cannot perform an aggregate function on an expression containing an aggregate or a subquery as an error error.
For others encountering the problem of using the SUM function inside of a MAX function, but not having the same issue as OP, based on Luv's answer, I was able to convert the following Scalar-valued function:
SELECT #MinMaxValue = MAX(SUM(ArtikelM.Omzet)/SUM(ArtikelM.Aantal))
FROM ArtikelM
INNER JOIN Klanten
ON ArtikelM.KlantenId = Klanten.KlantenId
WHERE ArtikelId = #ArtikelId
AND Klanten.KlantTypeCategorie = #KlantType
AND Klanten.KlantGrootteCategorie = #KlantGrootte
AND ArtikelM.Jaar = #Jaar
to
SELECT #MinMaxValue = MAX(selectedsum.sumx)
FROM (
SELECT SUM(ArtikelM.Omzet)/SUM(ArtikelM.Aantal) AS sumx
FROM ArtikelM
INNER JOIN Klanten
ON ArtikelM.KlantenId = Klanten.KlantenId
WHERE ArtikelId = #ArtikelId
AND Klanten.KlantTypeCategorie = #KlantType
AND Klanten.KlantGrootteCategorie = #KlantGrootte
AND ArtikelM.Jaar = #Jaar
) AS selectedsum
or if ArtikelM.Aantal could be zero
SELECT sumx =
CASE
WHEN SUM(ArtikelM.Aantal) > 0
THEN SUM(ArtikelM.Omzet)/SUM(ArtikelM.Aantal)
ELSE MIN(Leverancier.FactPrijs)
END
FROM ArtikelM
INNER JOIN Leverancier
ON ArtikelM.LevId = Leverancier.LevId
INNER JOIN Klanten
ON ArtikelM.KlantenId = Klanten.KlantenId
WHERE ArtikelId = #ArtikelId
AND Klanten.KlantTypeCategorie = #KlantType
AND Klanten.KlantGrootteCategorie = #KlantGrootte
AND ArtikelM.Jaar = #Jaar
) AS selectedsum
I think this might help out others too
Select max(product_id), min(product_id)
from Purchases Where Product_id In
(select product_id, Sum(quantity) from Purchases Group by product_id)
Related
If I remove the group by statement, then it is showing an error.
SELECT COMPANY.COMPANY_CODE,
company.founder,
count(distinct lm.lead_manager_code),
count(distinct sm.senior_manager_code),
count(distinct m.manager_code),
count(distinct e.employee_code)
from Company
join Lead_Manager as lm on Company.company_code = lm.company_code
join Senior_Manager as sm on Company.company_code = sm.company_code
join Manager as m on Company.company_code = m.company_code
join Employee as e on Company.company_code = e.company_code
GROUP BY Company.COMPANY_CODE, Company.FOUNDER
ORDER BY Company.COMPANY_CODE ASC
Basically, you are using an aggregate function so you need to group
select count(amount) from Orders group by customer_id
Orders
order_id item amount customer_id
1 Keyboard 400 4
2 Mouse 300 4
3 Monitor 12000 3
4 Keyboard 400 1
5 Mousepad 250 2
Now using group by on customer_id so we have id 4 so need to check the count of 4 id with group by
I have the following 2 select statements that work and return the info on their own, but was wondering if there was a way to join / combine them into one report with their own separate rows and columns
Select Sum (amount) as PDCGross
From Desk D Left Join
Master M
on D.code = M.desk INNER JOIN
pdc
ON m.number = pdc.number
Where teamid = 3
AND active = 1
And onhold is NULL
And deposit >='2020-10-01'
And deposit <= '2020-10-31'
Group by D.Name
Order by desk.Name Desc
Select Sum (amount) as GrossPDCC
FROM Desk D left JOIN
Master M
on D.code = M.Desk INNER JOIN
DebtorCreditCards P
ON m.number = P.Number
Where teamid = 3
AND IsActive = 1
And OnHoldDate is NULL
And DepositDate >='2020-10-01'
And DepositDate <= '2020-10-31'
Group by D.Name
Order by desk.Name Desc
The return for the 1st statement
PDCPross
2500
1500
1300
The result of the 2nd statement is
PDCCGross
1500
1300
1000
What i am looking for is
PDCPross PDCCGross
2500 1500
1500 1300
1300 1000
On MySQL v 8.0+ or MariaDB 10.2 +, you can use ROW_NUMBER() function:
Assign each query with ROW_NUMBER() then make both query as sub-query. Join the queries using the rownum result and you should be able to see your results side by side. Something like this query example:
SELECT A.PDCGross, B.GrossPDCC FROM
(SELECT ROW_NUMBER() OVER (ORDER BY D.Name DESC) AS rownum, SUM(amount) AS PDCGross
FROM Desk D LEFT JOIN
MASTER M
ON D.code = M.desk INNER JOIN
pdc
ON m.number = pdc.number
WHERE teamid = 3
AND active = 1
AND onhold IS NULL
AND deposit >='2020-10-01'
AND deposit <= '2020-10-31'
GROUP BY D.Name
ORDER BY D.Name DESC) A JOIN
(SELECT ROW_NUMBER() OVER (ORDER BY D.Name DESC) AS rownum, SUM(amount) AS GrossPDCC
FROM Desk D LEFT JOIN
MASTER M
ON D.code = M.Desk INNER JOIN
DebtorCreditCards P
ON m.number = P.Number
WHERE teamid = 3
AND IsActive = 1
AND OnHoldDate IS NULL
AND DepositDate >='2020-10-01'
AND DepositDate <= '2020-10-31'
GROUP BY D.Name
ORDER BY D.Name DESC) B ON A.rownum=B.rownum;
I have to 3 tables: product, product_to_store, store
product table
id quantity status
1 1 1
2 0 1
3 0 1
4 23 1
product_to_store table
store_id product_id
1 1
2 2
1 3
2 4
store table
id name
1 store1
2 store2
To find total products I can run query to fetch all products in table product where status of product is enabled.
select count(*) from product where status=1
total name
2 Store 1
2 store 2
To find total out of stock products I can run below query after joining all 3 tables and using group by store_id:
Select count(*) as outofproducts from product where quantity=0;
Result come like this:
outofproducts name
1 Store 1
1 store 2
But I want combination of above 2 results in single query like below:
outofproducts total name
1 2 Store 1
1 2 store 2
You'd use conditional aggregatiopn, i.e. sum/count over conditions:
select
s.name,
sum(p.quantity > 0) as in_stock,
sum(p.quantity = 0) as out_of_stock,
count(*) as total
from store s
join product_to_store ps on ps.store_id = s.id
join product p on p.id = ps.product_id
group by s.name
order by s.name;
This makes use of MySQL's true = 1, false = 0. If you don't like it, replace sum(p.quantity = 0) with sum(case when p.quantity = 0 then 1 else 0 end) or count(case when p.quantity = 0 then 1 end).
You can start query from store table so that we will get total rows as store table data.
Then use nested query for each store to get out of product and total product count
select
(Select count(*) as outofproducts from product_to_store ps inner join product p on p.id = ps.product_id where quantity=0 and ps.store_id = s.id ) as outofproducts ,
(Select count(*) as count from product_to_store ps inner join product p on p.id = ps.product_id where ps.store_id = s.id ) as totalCount,
s.name
from store s
You could join the related subquery for count
select t1.name, t1.outofproducts, t2.total
from(
select b.id, b.name , count(*) outofproducts
from product_to_store c
inner join product a on a.id = c.product_id
inner join store b on a.id = c.store_id
where a.quantity = 0
group by b.id, b.name
) t1
inner join (
select b.id, b.name , count(*) total
from product_to_store c
inner join product a on a.id = c.product_id
inner join store b on a.id = c.store_id
group by b.id, b.name
) t2 on t1.id = t2.id
I have an query like:
SELECT * FROM account AS a
LEFT JOIN (SELECT SUM(bill.amount) total, bill.accountId FROM bill GROUP BY bill.accountId) b ON a.id = b.accountId
WHERE a.partner_id = 1 OR a.partner_id = 2
How can I check, how many groups in "bill" has the same a.partner_id?
For example: 3 groups has partner_id = 1, 2 groups has partner_id = 2.
And later include to left join only groups, if more than 2 groups have the same partner_id.
If I understand correctly, you just want an aggregation on top of your query:
SELECT a.partner_id, count(*) as cnt, sum(total) as total
FROM account a LEFT JOIN
(SELECT SUM(b.amount) as total, b.accountId
FROM bill b
GROUP BY b.accountId
) b
ON a.id = b.accountId
GROUP BY a.partner_id;
You should be able to use the "HAVING" clause. Below is an example from the following link:
https://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
SELECT name, COUNT(name) AS c FROM orders
GROUP BY name
HAVING c = 1;
I have the following two queries, how can I join them together?
Query1:
SELECT product_name, count(product_name) as count_product_name
FROM ps_order_detail
WHERE id_shop = 1
group by product_name
order by count_product_name DESC
LIMIT 5
Query2:
SELECT count(*) as count, concat(decade, '-', decade + 9) as year
FROM (SELECT floor(year(`birthday`) / 10) * 10 as decade
FROM ps_customer) C
GROUP BY decade;
the first query is to get the top 5 product name that ordered the most.
the second query is get the customer birthday year and group them by every ten years.
I want to know the age group that order the top 5 product.
result should be
product name, years, count
productA 1990-2009 100
ProductA 2000-2019 20
ProductB 1980-1999 20
ProductB 1990-2009 25
ProductB 2000-2019 20
...
I do have a third table to have the connection.
create table ps_orders(
id_customer,
id_order
);
create table ps_customer(
id_customer, birthday
);
create table ps_order_detail(
id_order, product_name
);
I am not sure how to put them all together, I can input the product name one by one to get the years.
SELECT count(*) as count, concat(decade, '-', decade + 9) as year
FROM (SELECT floor(year(birthday) / 10) * 10 as decade
FROM ps_customer as CU,
ps_orders as O,
ps_order_detail as OD
WHERE CU.id_customer = O.id_customer
AND O.id_order = OD.id_order
AND OD.product_name = 'product Name A'
) C
GROUP BY decade;
Use your second query, but instead of specifying a particular product, put that into the grouping. Then join that with the subquery that returns the top 5 products to limit it.
SELECT product_name, concat(decade, '-', decade + 9) AS year, count(*) AS count
FROM (SELECT p.product_name, floor(year(birthday) / 10) * 10 as decade
FROM (SELECT product_name, count(*) as count_product_name
FROM ps_order_detail
WHERE id_shop = 1
group by product_name
order by count_product_name DESC
LIMIT 5) AS p
JOIN ps_order_detail AS od ON od.product_name = p.product_name
JOIN ps_orders AS o on o.id_order = od.id_order
JOIN ps_customer AS c ON c.id_customer = o.id_customer
WHERE od.id_shop = 1) AS x
GROUP BY product_name, decade