So I can run two separate queries like this:
SELECT date as date1, product as product1, product_id as product_id_1, SUM(revenue) AS rev1
FROM product_inventory
WHERE date = '2021-11-17'
GROUP BY date1 , product1, product_id_1
ORDER BY rev1 DESC
SELECT date as date2, product as product2, product_id as product_id_2, SUM(revenue) AS rev2
FROM product_inventory
WHERE date = '2022-11-17'
GROUP BY date2 , product2, product_id_2
ORDER BY rev2 DESC
And this is the output I get for each:
date1 | product1 | product_id_1 | rev1 |
---|---|---|---|
2021-11-17 | adidas samba | 9724 | 6087.7000732421875 |
2021-11-17 | nike air max | 5361 | 4918.0 |
2021-11-17 | puma suede | 1985 | 3628.1600341796875 |
date2 | product2 | product_id_2 | rev2 |
---|---|---|---|
2022-11-17 | adidas samba | 9724 | 5829.0 |
2022-11-17 | nike air max | 5361 | 4841.864013671875 |
2022-11-17 | puma suede | 1985 | 5404.4140625 |
How can I query the db in a way that would pull the date2 and rev2 column into one single output like this?
date1 | product1 | product_id_1 | rev1 | date2 | rev2 |
---|---|---|---|---|---|
2021-11-17 | adidas samba | 9724 | 6087.7000732421875 | 2022-11-17 | 5829.0 |
2021-11-17 | nike air max | 5361 | 4918.0 | 2022-11-17 | 4841.864013671875 |
2021-11-17 | puma suede | 1985 | 3628.1600341796875 | 2022-11-17 | 5404.4140625 |
I tried this query:
SELECT A.date1, A.product1, A.rev1, B.date2, B.product2, B.rev2 FROM
(
SELECT date as date1, product as product1, product_id as product_id_1, SUM(revenue) AS rev1 FROM product_inventory WHERE date = '2021-11-17' GROUP BY date1 , product1, product_id_1 ORDER BY rev1 DESC
) A,
(
SELECT date as date2, product as product2, product_id as product_id_2, SUM(revenue) AS rev2 FROM product_inventory WHERE date = '2022-11-17' GROUP BY date2, product2, product_id_2 ORDER BY rev2 DESC
) B;
but I get this output
date1 | product1 | rev1 | date2 | product2 | rev2 |
---|---|---|---|---|---|
2021-11-17 | puma suede | 3628.1600341796875 | 2022-11-17 | adidas samba shoes | 5829.0 |
2021-11-17 | nike air max | 4918.0 | 2022-11-17 | adidas samba shoes | 5829.0 |
2021-11-17 | adidas samba | 6087.7000732421875 | 2022-11-17 | adidas samba shoes | 5829.0 |
2021-11-17 | puma suede | 3628.1600341796875 | 2022-11-17 | puma suede | 5404.4140625 |
2021-11-17 | nike air max | 4918.0 | 2022-11-17 | puma suede | 5404.4140625 |
2021-11-17 | adidas samba | 6087.7000732421875 | 2022-11-17 | puma suede | 5404.4140625 |
2021-11-17 | puma suede | 3628.1600341796875 | 2022-11-17 | nike air max | 4841.864013671875 |
2021-11-17 | nike air max | 4918.0 | 2022-11-17 | nike air max | 4841.864013671875 |
2021-11-17 | adidas samba | 6087.7000732421875 | 2022-11-17 | nike air max | 4841.864013671875 |
It’s like the number of records gets squared.
4
Answers
Your first two queries are practically identical. Only the aliases in them are different. That makes the task a bit pointless. But if we assume you want to join the data from the two result sets, here is how you could do it:
Also, MySQL 8 introduced a feature called Common Table Expressions (CTE), which might be a nice alternative to the above query, in that it allows you to separate the SELECT queries of the two result sets you want to join. So with CTE you could write something like this:
Note that in both queries, I’ve extracted the
ORDER BY
from the two result sets and have put it in the "final" SELECT.You need to use JOIN on product_id like this:
Using your informations, you can do the
WITH
from sql, it will generate "fake table" that you can use to do some others operationYou want to compare the revenues of the same product on two different dates. You can do this without subqueries or
WITH
, using conditional aggregation:I don’t really see the need to return the dates in the resultset; they are know to the client already, since they are given as paramters to the query. But it you like, you either hardcode them in the
SELECT
clause, or useMIN
andMAX
.