How to select Group By groups which contain a specific value? - mysql

I have a table of orders which follows the below format:
╔══════════╦══════════╦══════════╦═══════╦══════════════╦═══════════════╗
║ Order ID ║ Subtotal ║ Shipping ║ Total ║ Product Name ║ Product Price ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1 ║ 30 ║ 5 ║ 35 ║ Apple ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1 ║ ║ ║ ║ Banana ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1 ║ ║ ║ ║ Coffee ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 2 ║ 30 ║ 5 ║ 35 ║ Peach ║ 20 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 2 ║ ║ ║ ║ Banana ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 3 ║ 20 ║ 3 ║ 23 ║ Peach ║ 20 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 4 ║ 40 ║ 10 ║ 50 ║ Apple ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 4 ║ ║ ║ ║ Coffee ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 4 ║ ║ ║ ║ Peach ║ 20 ║
╚══════════╩══════════╩══════════╩═══════╩══════════════╩═══════════════╝
All order numbers come as totals - subtotal and shipping with the lines below blank.
I am trying to look into the sales of a specific product.
For example I'd like to have a query which selects all orders which contain "Banana" and also returns the subtotal, shipping and total for the order as well as the product price (which are sometimes not on the same line):
I.e. it'd return this:
╔══════════╦══════════╦══════════╦═══════╦══════════════╦═══════════════╗
║ Order ID ║ Subtotal ║ Shipping ║ Total ║ Product Name ║ Product Price ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1 ║ 30 ║ 5 ║ 35 ║ Banana ║ 10 ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 2 ║ 30 ║ 5 ║ 35 ║ Banana ║ 10 ║
╚══════════╩══════════╩══════════╩═══════╩══════════════╩═══════════════╝

You can join the table to itself:
select
b.`Order ID`,
max(s.Subtotal) as 'Subtotal',
max(s.Shipping) as 'Shipping',
max(s.Total) as 'Total',
b.`Product Name`,
b.`Product Price`
from orders b
join orders s on s.`Order ID`=b.`Order ID`
where b.`Product Name`='Banana'
group by
b.`Order ID`,
b.`Product Name`,
b.`Product Price`
See a dbfiddle.

Related

Efficient use of Sum in query within the same id

I am learning sql query.
Here is my table simplified
╔════╦══════════════╦══════════════╦════════╦
║ id ║ user_id ║ department_id║ salary ║
╠════╬══════════════╬══════════════╬════════╬
║ 1 ║ 1 ║ 3 ║ 100 ║
║ 2 ║ 2 ║ 3 ║ 50 ║
║ 3 ║ 1 ║ 3 ║ 30 ║
║ 4 ║ 2 ║ 3 ║ 20 ║
║ 5 ║ 2 ║ 3 ║ 20 ║
╚════╩══════════════╩══════════════╩════════╩
Here is what I want to have below
╦══════════════╦══════════════╦════════╦
║ user_id ║ department_id║ salary ║
╠══════════════╬══════════════╬════════╬
║ 1 ║ 3 ║ 130 ║
║ 2 ║ 3 ║ 90 ║
╚══════════════╩══════════════╩════════╩
I want to add user salary if they are in the same department.
I have nooo ideas how to start.
Does anyone have good feedback that I can start with?
Thank you in advance
Try this query:
Select user_id, department_id, sum(salary) from table
Group by user_id, department_id
In the select clause, you have the columns you wish to select and your group by clause contains the grouping.

JOIN two tables and GROUP by the common column [duplicate]

This question already has answers here:
MySQL Results as comma separated list
(4 answers)
Closed 5 years ago.
I'm new to MYSQL and struggling to form a query that joins two tables and returns a unique product row.
TABLE product:
╔══════════════════════════════╦══════════╦════════════╦════════════════════╗
║ ref ║ brand ║ mpn ║ sku ║
╠══════════════════════════════╬══════════╬════════════╬════════════════════╣
║ 0001___DOGICLON___912-101242 ║ DOGICLON ║ 912-101242 ║ 000000000001082649 ║
║ 0002___DOGICLON___912-101242 ║ DOGICLON ║ 912-101242 ║ 912-101242 ║
║ 0003___Dogiclon___912-101242 ║ Dogiclon ║ 912-101242 ║ 912-101242(R400) ║
║ 0005___Dogiclon___912-101242 ║ Dogiclon ║ 912-101242 ║ MILT-R400 ║
╚══════════════════════════════╩══════════╩════════════╩════════════════════╝
TABLE inventory:
╔══════════════════════════════╦═══════╦═════════╦══════════╗
║ ref ║ scost ║ instock ║ location ║
╠══════════════════════════════╬═══════╬═════════╬══════════╣
║ 0001___DOGICLON___912-101242 ║ 53.68 ║ 24 ║ WA ║
║ 0001___DOGICLON___912-101242 ║ 53.68 ║ 0 ║ CA ║
║ 0002___DOGICLON___912-101242 ║ 61.00 ║ 121 ║ WA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 100 ║ WA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 0 ║ NY ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 20 ║ MA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 2 ║ CA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ IN ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ MA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ WA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ NY ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 2 ║ CA ║
╚══════════════════════════════╩═══════╩═════════╩══════════╝
I guess pseduo code would be:
SHOW all products
WHERE
instock (any location) > 0 AND
(cost > 10 AND cost < 2000)
ORDER BY
cost asc
Notes:
refs are unique per supplier
brand and mpn lookup needs to be
case insensitive
Expected Result:
╔══════════╦══════════╦════════════╦══════════════╦══════════════╦═══════════════════════════╗
║ ref ║ brand ║ mpn ║ sku ║ scost ║ instock ║
╠══════════╬══════════╬════════════╬══════════════╬══════════════╬═══════════════════════════╣
║ whatever ║ Dogiclon ║ 912-101242 ║ based on ref ║ based on ref ║ based on ref and location ║
╚══════════╩══════════╩════════════╩══════════════╩══════════════╩═══════════════════════════╝
This is what I'm trying:
SELECT DISTINCT
product.ref,
product.brand,
inventory.scost,
inventory.instock
FROM
product
JOIN inventory ON inventory.ref = product.ref
WHERE
inventory.instock > 1
AND ( app.inventory.scost >= 10 AND app.inventory.scost <= 2000 )
GROUP BY
product.ref
If you are grouping by unique column within product which seems to be ref, distinct won't do what you wish it would. DISTINCT takes care of uniqueness per returned row (all values being returned). In this case to get one row in result per product ref you'd need to drop the distinct part, add product.mfr to group by and aggregate non-grouped columns like below:
SELECT
product.ref,
product.mfr,
group_concat(inventory.scost) as scost,
group_concat(inventory.instock) as instock,
FROM
product
JOIN inventory ON inventory.ref = product.ref
WHERE
inventory.instock > 1
AND ( app.inventory.scost >= 10 AND app.inventory.scost <= 2000 )
GROUP BY
product.ref, product.mfr
If you want to ensure proper ordering for scost and instock just include ordering clause within the group_concat itself ie:
group_concat(column order by column1, [...])

Need help to retrieve data from three MySQL tables

I have three tables named "users","user_hobbies" and "hobbies". Below is the sample tables with values;Below is the users table with fields id, name and age
╔════╦══════╦═════╗
║ ID ║ NAME ║ AGE ║
╠════╬══════╬═════╣
║ 1 ║ abc ║ 23 ║
║ 2 ║ xyz ║ 24 ║
║ 3 ║ pqr ║ 21 ║
╚════╩══════╩═════╝
and below is user_hobbies table with fields id, user_id and hobby_id
╔════╦═════════╦══════════╗
║ ID ║ USER_ID ║ HOBBY_ID ║
╠════╬═════════╬══════════╣
║ 1 ║ 1 ║ 1 ║
║ 2 ║ 1 ║ 2 ║
║ 3 ║ 1 ║ 3 ║
║ 4 ║ 2 ║ 4 ║
║ 5 ║ 2 ║ 3 ║
║ 6 ║ 2 ║ 5 ║
║ 7 ║ 3 ║ 2 ║
║ 8 ║ 4 ║ 6 ║
╚════╩═════════╩══════════╝
. Below is the hobbies table with fields id and desc
╔════╦═══════════╗
║ ID ║ DESC ║
╠════╬═══════════╣
║ 1 ║ music ║
║ 2 ║ chatting ║
║ 3 ║ cricket ║
║ 4 ║ badminton ║
║ 5 ║ chess ║
║ 6 ║ cooking ║
╚════╩═══════════╝
. The actual requirement is that i need a query to retrieve name, age, hobby_id and desc (see an example below)
╔══════╦═════╦══════════╦═════════════════════════╗
║ NAME ║ AGE ║ HOBBYID ║ DESC ║
╠══════╬═════╬══════════╬═════════════════════════╣
║ abc ║ 23 ║ 1,2,3 ║ music,chatting,cricket ║
║ pqr ║ 21 ║ 2 ║ chatting ║
║ xyz ║ 24 ║ 4,3,5 ║ badminton,cricket,chess ║
╚══════╩═════╩══════════╩═════════════════════════╝
You need to join the tables first and use an aggregate function called GROUP_CONCAT().
SELECT a.Name,
a.Age,
GROUP_CONCAT(c.ID) hobbyIDs,
GROUP_CONCAT(c.desc) descList
FROM users a
INNER JOIN user_hobbies b
ON a.ID = b.user_ID
INNER JOIN hobbies c
ON b.hobby_ID = c.ID
GROUP BY a.Name, a.Age
SQLFiddle Demo
MySQL GROUP_CONCAT()
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
OUTPUT
╔══════╦═════╦══════════╦═════════════════════════╗
║ NAME ║ AGE ║ HOBBYIDS ║ DESCLIST ║
╠══════╬═════╬══════════╬═════════════════════════╣
║ abc ║ 23 ║ 1,2,3 ║ music,chatting,cricket ║
║ pqr ║ 21 ║ 2 ║ chatting ║
║ xyz ║ 24 ║ 4,3,5 ║ badminton,cricket,chess ║
╚══════╩═════╩══════════╩═════════════════════════╝

Multiple column in order by clause - mysql

I have two columns in a table say, LIKE and FAVORITES (int value)
See the chart:
╔════╦══════╦══════════╗
║ ID ║ LIKE ║ FAVORITE ║
╠════╬══════╬══════════╣
║ 1 ║ 25 ║ 9 ║
║ 2 ║ 5 ║ 17 ║
║ 3 ║ 6 ║ 1 ║
║ 4 ║ 45 ║ 0 ║
║ 5 ║ 3 ║ 44 ║
╚════╩══════╩══════════╝
Now, I want to select the Maximum Like and Favorites IDs from the SELECT clause.
I have tried
SELECT ID from TABLE WHERE CONDITION ORDER BY LIKE,FAVORITES DESC
But the result shows the rows based on LIKE DESC order.
The result should be
╔════╗
║ ID ║
╠════╣
║ 5 ║
║ 4 ║
║ 1 ║
║ 2 ║
║ 3 ║
╚════╝
I think you need to add those two columns. eg,
SELECT ID
FROM tableName
ORDER BY `LIKE` + FAVORITE DESC
SQLFiddle Demo
Result:
╔════╗
║ ID ║
╠════╣
║ 5 ║
║ 4 ║
║ 1 ║
║ 2 ║
║ 3 ║
╚════╝

Join table to get the latest rows from unique row

I have two table
The table structure goes something like this
Table A
╔══════╦════════╗
║ P_ID ║ P_NAME ║
╠══════╬════════╣
║ 1 ║ name1 ║
║ 2 ║ name2 ║
║ 3 ║ name3 ║
║ 4 ║ name5 ║
╚══════╩════════╝
Table B
╔═════╦════════╦════════╦═══════════╦═══════╗
║ ID ║ P_ID ║ C_ID ║ C_PRICE ║ TIME ║
╠═════╬════════╬════════╬═══════════╬═══════╣
║ 1 ║ 1 ║ 3 ║ 11 ║ 11111 ║
║ 2 ║ 2 ║ 4 ║ 22 ║ 22222 ║
║ 3 ║ 3 ║ 5 ║ 33 ║ 33333 ║
║ 4 ║ 3 ║ 6 ║ 44 ║ 44444 ║
║ 5 ║ 3 ║ 6 ║ 55 ║ 55555 ║
║ 6 ║ 4 ║ 7 ║ 66 ║ 66666 ║
╚═════╩════════╩════════╩═══════════╩═══════╝
The requirement is
1. Join two table
2. Group By C_ID
3. The latest rows in Group By
I tried to modify the answer By Bill Karwin from How do I join the most recent row in one table to another table?
SELECT e.*, s1.*
FROM table_a e
INNER JOIN
table_b s1
ON (e.p_id = s1.p_id)
LEFT OUTER JOIN table_b s2
ON (e.p_id = s2.p_id AND s1.id < s2.id)
WHERE s2.p_id IS NULL;
but I could not achieve what I want. From his answer I will get
╔═════╦════════╦════════╦═══════════╦═══════╦═════════╗
║ ID ║ P_ID ║ C_ID ║ C_PRICE ║ TIME ║ P_NAME ║
╠═════╬════════╬════════╬═══════════╬═══════╬═════════╣
║ 1 ║ 1 ║ 3 ║ 11 ║ 11111 ║ name1 ║
║ 2 ║ 2 ║ 4 ║ 22 ║ 22222 ║ name2 ║
║ 5 ║ 3 ║ 6 ║ 55 ║ 55555 ║ name3 ║
║ 6 ║ 4 ║ 7 ║ 66 ║ 66666 ║ name5 ║
╚═════╩════════╩════════╩═══════════╩═══════╩═════════╝
But the output what I want is as below ( dublicate P_ID is ok but for each dublicate P_ID should not have dublicate C_ID )
╔═════╦════════╦════════╦═══════════╦═══════╦═════════╗
║ ID ║ P_ID ║ C_ID ║ C_PRICE ║ TIME ║ P_NAME ║
╠═════╬════════╬════════╬═══════════╬═══════╬═════════╣
║ 1 ║ 1 ║ 3 ║ 11 ║ 11111 ║ name1 ║
║ 2 ║ 2 ║ 4 ║ 22 ║ 22222 ║ name2 ║
║ 3 ║ 3 ║ 5 ║ 33 ║ 33333 ║ name3 ║
║ 5 ║ 3 ║ 6 ║ 55 ║ 55555 ║ name3 ║
║ 6 ║ 4 ║ 7 ║ 66 ║ 66666 ║ name5 ║
╚═════╩════════╩════════╩═══════════╩═══════╩═════════╝
SELECT e.*, s1.*
FROM table_a e INNER JOIN
(SELECT * FROM (SELECT * FROM table_b ORDER BY time DESC) temp GROUP BY c_id) s1;
The innermost ORDER BY ensures the for the same c_id the desired row always comes first, the outter GROUP BY will group on c_id, and having not specified otherwise will return the first row found for each group.
You must
Join two table
Group By C_ID
Number row for each group desc ordered by chosen field (last imply order!)
Filter by row number = 1.
This can help you for numbering rows.