MYSQL: Separate products into columns - mysql

This is in relation to my previous post MYSQL: Get quantity per product in each order.
Using the query below, it generates a table of:
SELECT o.order_id, o.username, op.product_id, SUM( op.quantity ) as quantity
FROM `orders` o JOIN
orders_products op
ON op.order_id = o.order_id
GROUP BY op.product_id, o.order_id
ORDER BY o.order_id, op.product_id;
_______________________________________________
| order_id | username | product_id | quantity |
-----------------------------------------------
| 1 | bill | 1 | 5 |
-----------------------------------------------
| 1 | bill | 2 | 3 |
-----------------------------------------------
| 1 | bill | 3 | 2 |
-----------------------------------------------
| 2 | sally | 1 | 2 |
-----------------------------------------------
| 3 | jeff | 1 | 6 |
-----------------------------------------------
| 3 | jeff | 2 | 7 |
-----------------------------------------------
As seen above, each new product_id is assigned its own row. Is it possible to have additional columns for each product_id instead of rows generating an output same as the one below?
_______________________________________________________________________
| order_id | username | product_qty_1 | product_qty_2 | product_qty_3 |
-----------------------------------------------------------------------
| 1 | bill | 5 | 3 | 2 |
-----------------------------------------------------------------------
| 2 | sally | 2 | 0 | 0 |
-----------------------------------------------------------------------
| 3 | jeff | 6 | 7 | 0 |
-----------------------------------------------------------------------
My tables:
-- Orders
_______________________
| order_id | username |
-----------------------
| 1 | bill |
-----------------------
| 2 | sally |
-----------------------
| 3 | jeff |
-----------------------
-- Products
___________________
| id | product |
-------------------
| 1 | Table |
-------------------
| 2 | Chair |
-------------------
| 3 | Mouse |
-------------------
-- Order Products
___________________________________________
| id | order_id | product_id | quantity |
-------------------------------------------
| 1 | 1 | 1 | 5 |
-------------------------------------------
| 2 | 1 | 2 | 3 |
-------------------------------------------
| 3 | 1 | 3 | 2 |
-------------------------------------------
| 4 | 2 | 1 | 2 |
-------------------------------------------
| 5 | 3 | 1 | 6 |
-------------------------------------------
| 6 | 3 | 2 | 7 |
-------------------------------------------

If you know the products in advance, you can use conditional aggregation:
SELECT o.order_id, o.username,
SUM(CASE WHEN op.product_id = 1 THEN op.quantity ELSE 0 END) as quantity1,
SUM(CASE WHEN op.product_id = 2 THEN op.quantity ELSE 0 END) as quantity2,
SUM(CASE WHEN op.product_id = 3 THEN op.quantity ELSE 0 END) as quantity3
FROM `orders` o JOIN
orders_products op
ON op.order_id = o.order_id
GROUP BY o.order_id
ORDER BY o.order_id;
If you don't know the product ids, then you would have to use dynamic SQL, or perhaps use GROUP_CONCAT() to create a string with the information you want.

Related

Get quantity per product in each order using MySQL

I have 3 tables, order, products, and orders_products and I'm trying to find out how many of each product was bought in each order. Is it possible to get all information in a single query?
My current query doesn't seem to work, whereas the product_id comes out as all the same.
SELECT orders.order_id, orders.username, COUNT( DISTINCT op.product_id ) product_id, SUM( op.quantity ) quantity
FROM `orders`
JOIN orders_products op ON op.order_id = orders.order_id
GROUP BY product_id, orders.order_id ORDER BY order_id, product_id
Which results in:
_______________________________________________
| order_id | username | product_id | quantity |
-----------------------------------------------
| 1 | bill | 1 | 3 |
-----------------------------------------------
| 1 | bill | 1 | 2 |
-----------------------------------------------
| 1 | bill | 1 | 5 |
-----------------------------------------------
| 2 | sally | 1 | 2 |
-----------------------------------------------
| 3 | jeff | 1 | 6 |
-----------------------------------------------
| 3 | jeff | 1 | 7 |
-----------------------------------------------
You can see the problem above in the product_id column which is always set to 1.
I'm trying to get something like this:
_______________________________________________
| order_id | username | product_id | quantity |
-----------------------------------------------
| 1 | bill | 1 | 5 |
-----------------------------------------------
| 1 | bill | 2 | 3 |
-----------------------------------------------
| 1 | bill | 3 | 2 |
-----------------------------------------------
| 2 | sally | 1 | 2 |
-----------------------------------------------
| 3 | jeff | 1 | 6 |
-----------------------------------------------
| 3 | jeff | 2 | 7 |
-----------------------------------------------
My tables:
-- Orders
_______________________
| order_id | username |
-----------------------
| 1 | bill |
-----------------------
| 2 | sally |
-----------------------
| 3 | jeff |
-----------------------
-- Products
___________________
| id | product |
-------------------
| 1 | Table |
-------------------
| 2 | Chair |
-------------------
| 3 | Mouse |
-------------------
-- Order Products
___________________________________________
| id | order_id | product_id | quantity |
-------------------------------------------
| 1 | 1 | 1 | 5 |
-------------------------------------------
| 2 | 1 | 2 | 3 |
-------------------------------------------
| 3 | 1 | 3 | 2 |
-------------------------------------------
| 4 | 2 | 1 | 2 |
-------------------------------------------
| 5 | 3 | 1 | 6 |
-------------------------------------------
| 6 | 3 | 2 | 7 |
-------------------------------------------
I think the query you want is:
SELECT o.order_id, o.username, op.product_id, SUM( op.quantity ) as quantity
FROM `orders` o JOIN
orders_products op
ON op.order_id = o.order_id
GROUP BY op.product_id, o.order_id
ORDER BY o.order_id, op.product_id;
In general, you do want all the columns in the GROUP BY in the SELECT. But (in general) they shouldn't be arguments to aggregation functions such as COUNT().
You can see the problem above in the product_id column which is always
set to 1
I think you are bit confused here, that's the data present in Order Products table for product_id column but in your query what you are trying to get is count of data COUNT( DISTINCT op.product_id ) product_id and since the count is 1, the result is showing the same.

LeftJoin Minus (Right Table data + inner join Data)

TABLE 1)
m_conservationsetting
+------------+-------+------------+
| FacilityId | Unit | CategoryId |
+------------+-------+------------+
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
| 1 | 2 | 1 |
| 1 | 2 | 2 |
| 2 | 1 | 1 |
| 2 | 2 | 1 |
+------------+-------+------------+
Unique Key(FacilityId Unit CategoryId)
TABLE 2)
l_maintelog
+------------+------+------------+--------+
| FacilityId | Unit | CategoryId | Status |
+------------+------+------------+--------+
| 1 | 1 | 1 | 0 |
| 1 | 1 | 2 | 1 |
| 1 | 1 | 3 | 0 |
| 1 | 2 | 1 | 0 |
| 2 | 1 | 1 | 0 |
| 2 | 2 | 1 | 0 |
+------------+------+------------+--------+
Result :
+------------+------+------------+
| FacilityId | Unit | CategoryId |
+------------+------+------------+
| 1 | 1 | 2 |
| 1 | 2 | 2 |
+------------+------+------------+
Table1 need to be left Joined with Table2 and it should ommit the join results and show only table1 data as results.
Table1 LeftJoin Table2 - (join Data) for the below query. The condition for getting result is to check the status=0 for the record in Table2
Query
select cs.FacilityId,Cs.Unit,cs.CategoryId
from m_conservationsetting cs
LEFT JOIN l_maintelog ml on
(cs.FacilityId=ml.FacilityId and cs.Unit=ml.Unit)
WHERE (ml.Status=0
)
group by cs.CategoryId
You can use NULL conditional on Second Table's Key column to eliminate result which is there in both tables
Select cs.FacilityId,Cs.Unit,cs.CategoryId
from m_conservationsetting cs
LEFT JOIN l_maintelog ml on
(cs.FacilityId=ml.FacilityId and cs.Unit=ml.Unit)
WHERE (ml.Status=0
and ml.FacilityId IS NULL)
group by cs.CategoryId

MySQL combine multiple rows into one and maintain column names

The Tables
The tables and some dummy data to illustrate the issue.
members
Stores basic information about the members.
------------------------------------
| member_id | email |
------------------------------------
| 1 | 1#a.com |
------------------------------------
| 2 | 2#a.com |
------------------------------------
| 3 | 3#a.com |
------------------------------------
| 4 | 4#a.com |
------------------------------------
| 5 | 4#a.com |
------------------------------------
member_data
Stores some extra meta data for each member
----------------------------------------
| member_id | name | surname | company |
----------------------------------------
| 1 | A | A | A |
----------------------------------------
| 2 | B | B | B |
----------------------------------------
| 3 | C | C | C |
----------------------------------------
| 4 | D | D | D |
----------------------------------------
| 5 | E | E | E |
----------------------------------------
categories
Different categories that are within the system.
------------------------------------
| cat_id | cat_name |
------------------------------------
| 1 | Cars |
------------------------------------
| 2 | Bikes |
------------------------------------
| 3 | Boats |
------------------------------------
licences
A member must have a licence to be able to access a category.
-----------------------------------------------------------------------
| id | subid | catid | start_date | end_date | description |
-----------------------------------------------------------------------
| 1 | 1 | 1 | 2014-01-01 | 2020-12-31 | Premium |
-----------------------------------------------------------------------
| 2 | 1 | 2 | 2014-01-01 | 2015-12-31 | Premium |
-----------------------------------------------------------------------
| 3 | 1 | 3 | 2014-01-01 | 2018-12-31 | Premium |
-----------------------------------------------------------------------
| 4 | 2 | 1 | 2014-01-01 | 2016-12-31 | Premium |
-----------------------------------------------------------------------
| 7 | 3 | 1 | 2014-01-01 | 2014-01-02 | Premium |
-----------------------------------------------------------------------
| 8 | 3 | 2 | 2014-01-01 | 2014-01-02 | Premium |
-----------------------------------------------------------------------
| 9 | 3 | 3 | 2014-01-01 | 2020-01-31 | Premium |
-----------------------------------------------------------------------
| 10 | 5 | 1 | 2014-01-01 | 2014-01-02 | Premium |
-----------------------------------------------------------------------
| 11 | 5 | 2 | 2014-01-01 | 2014-01-02 | Premium |
-----------------------------------------------------------------------
| 12 | 5 | 3 | 2014-01-01 | 2014-01-02 | Premium |
-----------------------------------------------------------------------
About the data
Member 1 has a licence to categories 1,2 and 3. They are all active and valid.
Member 2 has a licence to category 1 only. It is active.
Member 3 has a licence to 1,2 and 3. Only the licence for category 3 is valid.
Member 4 has no licences.
Member 5 has licences for categories 1,2 and 3 but they are all expired.
What I'm Trying To Acheive
I want to get the records for each members' licence, with their respective member_data and category. A licence must exist and be valid for a category for the member to have data returned for that licence.
Furthermore, I want each of the licences that are returned to come back as one row, which contains all of the data required in the following format:
Output format
I want to output the members who hold valid licences, and return either their expiry date for a category or no output if they don't have a licence for a category but do hold one for another. I.e.:
----------------------------------------------------------------------------------
| Company | Name | LicenceType | Cars | Bikes | Boats |
----------------------------------------------------------------------------------
| A |A A | Premium |2020-12-31 | 2015-12-31 | 2018-12-21 |
----------------------------------------------------------------------------------
| B |B B | Premium |2016-12-31 | | |
----------------------------------------------------------------------------------
| C |C C | Premium | | | 2020-01-31 |
----------------------------------------------------------------------------------
What I've Tried
SELECT
md.company as Company,
CONCAT(md.name,' ', md.surname) as Name,
l.description as LicenceType,
(CASE WHEN (c.cat_name='Cars') THEN l.end_date ELSE '' END)AS Cars,
(CASE WHEN (c.cat_name='Bikes') THEN l.end_date ELSE '' END)AS Bikes,
(CASE WHEN (c.cat_name='Boats') THEN l.end_date ELSE '' END)AS Boats
FROM
licences as l
JOIN
categories as c ON c.cat_id=l.catid
JOIN
member_data as md ON md.member_id=l.subid
WHERE
l.end_date>='2014-12-17'
AND
(l.description='Premium')
ORDER BY Company ASC
Currently outputs
This is how the data currently appears:
----------------------------------------------------------------------------------
| Company | Name | LicenceType | Cars | Bikes | Boats |
----------------------------------------------------------------------------------
| A |A A | Premium |2020-12-31 | | |
----------------------------------------------------------------------------------
| A |A A | Premium | | 2015-12-31 | |
----------------------------------------------------------------------------------
| A |A A | Premium | | | 2018-12-21 |
----------------------------------------------------------------------------------
| B |B B | Premium |2016-12-31 | | |
----------------------------------------------------------------------------------
| C |C C | Premium | | | 2020-01-31 |
----------------------------------------------------------------------------------
The issue is, as you can see for Company A's record, showing as three distinct rows. I would like to have each of the three rows returned as just a single row, as per the output format shown above.
I'd appreciate any ideas on how to achieve this. Thank you.
Use aggregation:
SELECT
md.company as Company,
CONCAT(md.name,' ', md.surname) as Name,
l.description as LicenceType,
MAX(CASE WHEN (c.cat_name='Cars') THEN l.end_date ELSE '' END)AS Cars,
MAX(CASE WHEN (c.cat_name='Bikes') THEN l.end_date ELSE '' END)AS Bikes,
MAX(CASE WHEN (c.cat_name='Boats') THEN l.end_date ELSE '' END)AS Boats
FROM
licences as l
JOIN
categories as c ON c.cat_id=l.catid
JOIN
member_data as md ON md.member_id=l.subid
WHERE
l.end_date>='2014-12-17'
AND
(l.description='Premium')
GROUP BY Company, Name, l.description
ORDER BY Company ASC;

Don't know how to merge two MySQL tables

I'm new in SQL queries. I have a problem with the query.
I have table books:
+----+-------+--------+-----------+
| id | title | author | publisher |
+----+-------+--------+-----------+
| 1 | Book1 | 1 | 1 |
| 2 | Book2 | 1 | 2 |
| 3 | Book3 | 2 | 1 |
| 4 | Book4 | 2 | 2 |
| 5 | Book5 | 2 | 3 |
+----+-------+--------+-----------+
And I'm have another table, which contains copies of books with given book_id.
+----+---------+
| id | book_id |
+----+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 4 |
| 8 | 4 |
| 9 | 5 |
+----+---------+
All I need is to merge these two tables into one to have table sorted by amount of book copies.
I found solution to get table sorted by amount of book copies:
select book_copies.book_id, count(*) total_count
from book_copies
group by book_id
having count(*) > 0
order by count(*) desc;
+---------+-------------+
| book_id | total_count |
+---------+-------------+
| 1 | 3 |
| 2 | 2 |
| 4 | 2 |
| 3 | 1 |
| 5 | 1 |
+---------+-------------+
Now I don't know how to merge them.
I've tried like this:
select books.title from books
left join
(select book_copies.book_id, count(*) total_count
from book_copies
group by book_id
having count(*) > 0
order by count(*) desc)
as total_table on books.id = total_table.book_id;
But all I get was this:
+-------+
| title |
+-------+
| Book1 |
| Book2 |
| Book3 |
| Book4 |
| Book5 |
+-------+
Could you help me please?
EDIT: by merging I meant smth like this:
+-------+--------+-----------+-----+
| title | author | publisher | tot |
+-------+--------+-----------+-----+
| Book1 | 1 | 1 | 3 |
| Book2 | 1 | 2 | 2 |
| Book4 | 2 | 2 | 2 |
| Book3 | 2 | 1 | 1 |
+-------+--------+-----------+-----+
Many thanks to #Marco for the answer!
Dmitriy
I think you could try:
SELECT b.title, b.author, b.publisher, COUNT(bc.book_id) AS tot
FROM books b LEFT JOIN book_copies bc
ON b.id = bc.book_id
GROUP BY b.id
EDITED:
If you want sort, you can try
SELECT * FROM
(SELECT b.title, b.author, b.publisher, COUNT(bc.book_id) AS tot
FROM books b LEFT JOIN book_copies bc
ON b.id = bc.book_id
GROUP BY b.id) g
ORDER BY g.tot DESC

How to return row of sum()s

I now find my original table structure was not good, so want to change it.
But I am having a hard time designing queries to obtain totals in rows with the new structure.
current structure:
+----------+-------+-------+-------+-------+
| state | shop | item0 | item1 | item2 |
+----------+-------+-------+-------+-------+
| 5 | 0 | 1 | 2 | 3 |
| 5 | 1 | 1 | 2 | 3 |
| 5 | 2 | 1 | 2 | 3 |
| 4 | 3 | 1 | 2 | 3 |
+----------+-------+-------+-------+-------+
(quantities of items at shop)
I want to change to these 2 tables:
shops table
+---------+--------+
| shop_id | state |
+---------+--------+
| 0 | 5 |
| 1 | 5 |
| 2 | 5 |
| 3 | 4 |
+---------+--------+
items table
+------------+--------------+
| shop | item | quantity |
+------------+--------------+
| 0 | 0 | 1 |
| 0 | 1 | 2 |
| 0 | 2 | 3 |
| 1 | 0 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 2 | 0 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 0 | 1 |
| 3 | 1 | 2 |
| 3 | 2 | 3 |
+------------+--------------+
The old layout allowed simple queries for getting totals by row:
SELECT state,SUM(item0) t0,SUM(item1) t1,SUM(item2) t2
FROM shops
WHERE state=5
+--------+---------+---------+----------+
| state | t0 | t1 | t2 |
+--------+---------+---------+----------+
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
With the new structure,
I can get the totals in column as follows:
SELECT item,SUM(quantity) total
FROM shops
LEFT JOIN items ON shop=shopid
WHERE state=5
GROUP by item
+--------+---------+
| item | total |
+--------+---------+
| 0 | 3 |
+--------+---------+
| 1 | 6 |
+--------+---------+
| 2 | 9 |
+--------+---------+
but how do I get the totals in rows:
+--------+---------+---------+----------+
| state | t0 | t1 | t2 |
+--------+---------+---------+----------+
| 4 | 1 | 2 | 3 |
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
You might try using a few more JOINs:
SELECT S.state,
SUM(T0.quantity) AS "T0",
SUM(T1.quantity) AS "T1",
SUM(T2.quantity) AS "T2"
FROM shops AS S
LEFT JOIN items AS T0 ON S.shop_id = T0.shop_id AND T0.item=0
LEFT JOIN items AS T1 ON S.shop_id = T1.shop_id AND T1.item=1
LEFT JOIN items AS T2 ON S.shop_id = T2.shop_id AND T2.item=2
GROUP BY S.state
There might be an easier way.