I have the following query that is grabbing data from a table and combining all the products into one column and counting the quantity of that order.
I need to add an additional column to the output though, how can I also add ship_date?
select order_id, group_concat(product_id, ' - ', cnt separator ', ') as products, sum(cnt) as total
from (select order_id, product_id, count(*) as cnt
from tt_order_items
group by order_id, product_id
) op
group by order_id;
This is how the original table is laid out:
And this is how the query outputs the results:
How can I add ship_date to that output?
It looks like ship_date is fixed for each and every order_id. If so, you can just add it to the inner and outer aggregation:
select
order_id,
group_concat(product_id, ' - ', cnt separator ', ') as products,
sum(cnt) as total,
ship_date
from (
select order_id, product_id, count(*) as cnt, ship_date
from tt_order_items
group by order_id, product_id, ship_date
) op
group by order_id, ship_date;
Related
There is a table like this, how can I get customer_id, amount_spent, top_item out of it?
amount_spent the amount spent on all items by this customer
top_item, which displays the name of the item for which the customer has spent the most money
I have tried the following query, however I cannot output the top_1 item with it
select customer_id, sum(item_number * item_price) as amount_spent_1m
from temp
group by customer_id
Check the demo here.
You can achieve it as below :
select customer_id, sum(item_number * item_price) as amount_spent_1m,
item_name as top_item_1m
from temp
group by customer_id, item_name
order by amount_spent_1m desc;
It gives me the following result :
I am sure there is a way to do this with one less step but my brain is not seeing it right now -
SELECT customer_id, amount_spent, item_name AS top_item
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY item_total DESC) rn,
SUM(item_total) OVER (PARTITION BY customer_id) amount_spent
FROM (
SELECT customer_id, item_id, item_name, SUM(item_price * item_number) item_total
FROM table1
GROUP BY customer_id, item_id, item_name
) t1
) t2
WHERE rn = 1
db<>fiddle
I've got the following order table:
Client_name|Product Megane|#768_Samsung Megane|#310_Apple
Megane|#659_Samsung Victor|#890_Apple
I'd like to see for each client what's their most bought brand + the number of time they bought it.
So for this table, I'd like this result:
Client_Name|Favourite_brand|Order_number Megan|Samsung|2
Victor|Apple|1
So far this is the query I've built:
SELECT Client_name, brand
FROM (SELECT Client_name, brand, ROW_NUMBER() OVER (PARTITION BY Client_name ORDER BY freq DESC) AS rn
FROM ( SELECT Client_name, brand, COUNT('x') AS freq
FROM (SELECT Client_name, substring(Product,5) as brand
FROM Orders
GROUP BY Client_name, brand) frequency) ranked) client
WHERE rn = 1;
I am still struggling with removing the number before the brand name (ie #768 for instance) by using substring. I shows me an error message
-ERROR: column "frequency.Client_name" must appear in the GROUP BY clause or be used in an aggregate function
So I haven't started yet to think how to add the Order_number column
Your help is highly appreciated!
I think you just have your group by clause in the wrong spot. Try moving it to after the 'frequency' but before the ')'
SELECT Client_name, brand
FROM (SELECT Client_name, brand, ROW_NUMBER() OVER (PARTITION BY Client_name ORDER BY freq DESC) AS rn
FROM ( SELECT Client_name, brand, COUNT('x') AS freq
FROM (SELECT Client_name, substring(Product,5) as brand
FROM Orders) frequency GROUP BY Client_name, brand) ranked) client
WHERE rn = 1;
I have a table with
orderNumber(pk) , customerNumber , comment
I have to count the maximum order placed by a user and show its user ID and MAX count . I have following Query
It shows the count Right but it takes the first CustomerNumber in the table
SELECT maxCount.customerNumber , MAX(`counted`) FROM
(
SELECT customerNumber, COUNT(*) AS `counted`
FROM `orders`
GROUP BY `customerNumber`
)as maxCount
Thanks & regards
Just use ORDER BY with your inner query:
SELECT customerNumber, COUNT(*) AS `counted`
FROM `orders`
GROUP BY `customerNumber`
ORDER BY COUNT(*) DESC
LIMIT 1
If you want to return all customer numbers in the event of a tie, you can use a HAVING clause with a subquery which identifies the maximum count:
SELECT customerNumber, COUNT(*) AS counted
FROM orders
GROUP BY customerNumber
HAVING COUNT(*) = (SELECT MAX(t.counted) FROM (SELECT COUNT(*) AS counted
FROM orders
GROUP BY customerNumber) t)
Demo here:
SQLFiddle
SQL Fiddle
Table scheme:
CREATE TABLE company
(`company_id` int,`name` varchar(30))
;
INSERT INTO company
(`company_id`,`name`)
VALUES
(1,"Company A"),
(2,"Company B")
;
CREATE TABLE price
(`company_id` int,`price` int,`time` timestamp)
;
INSERT INTO price
(`company_id`,`price`,`time`)
VALUES
(1,50,'2015-02-21 02:34:40'),
(2,60,'2015-02-21 02:35:40'),
(1,70,'2015-02-21 05:34:40'),
(2,120,'2015-02-21 05:35:40'),
(1,150,'2015-02-22 02:34:40'),
(2,130,'2015-02-22 02:35:40'),
(1,170,'2015-02-22 05:34:40'),
(2,190,'2015-02-22 05:35:40')
I'm using Cron Jobs to fetch company prices. In concatenating the price history for each company, how can I make sure that only the last one in each day is included? In this case, I want all of the price records around 05:30am concatenated.
This is the result I'm trying to get (I have used Date(time) to only get the dates from the timestamps):
COMPANY_ID PRICE TIME
1 70|170 2015-02-21|2015-02-22
2 120|190 2015-02-21|2015-02-22
I have tried the following query but it doesn't work. The prices don't correspond to the dates and I don't know how to exclude all of the 2:30 am records before applying the Group_concat function.
SELECT company_id,price,trend_date FROM
(
SELECT company_id, GROUP_CONCAT(price SEPARATOR'|') AS price,
GROUP_CONCAT(trend_date SEPARATOR'|') AS trend_date
FROM
(
SELECT company_id,price,
DATE(time) AS trend_date
FROM price
ORDER BY time ASC
)x1
GROUP BY company_id
)t1
Can anyone show me how to get the desired result?
Ok, so this should work as intended:
SELECT p.company_id,
GROUP_CONCAT(price SEPARATOR '|') as price,
GROUP_CONCAT(PriceDate SEPARATOR '|') as trend_date
FROM price as p
INNER JOIN (SELECT company_id,
DATE(`time`) as PriceDate,
MAX(`time`) as MaxTime
FROM price
GROUP BY company_id,
DATE(`time`)) as t
ON p.company_id = t.company_id
AND p.`time` = t.MaxTime
GROUP BY p.company_id
Here is the modified sqlfiddle.
This is a bit unorthodox but I think it solves your problem:
SELECT company_id,
GROUP_CONCAT(price SEPARATOR'|'),
GROUP_CONCAT(trend_date SEPARATOR'|')
FROM (
SELECT *
FROM (
SELECT company_id,
DATE(`time`) `trend_date`,
price
FROM price
ORDER BY `time` DESC
) AS a
GROUP BY company_id, `trend_date`
) AS b
GROUP BY company_id
I have a mysql table like this
id |code|price|quantity
1 |0001|10.00|1
2 |0001|10.00|1
3 |0001|15.00|3
4 |0001|15.00|1
5 |0002| 5.00|2
6 |0002| 5.00|1
Using the functions concat and group_concat I got this result:
SELECT code, group_concat( CONCAT( quantity, 'x', FORMAT(price, 2 ))SEPARATOR '+' ) AS sales
FROM detalle_sales
GROUP BY code
code|sales
0001|1x10,00+1x10,00+3x15.00+1x15.00
0002|2x5.00+1x5.00
How can I group the quantities by the price using mysql?
code|sales
0001|2x10.00+4x15.00
0002|3x5.00
I've done it by php but when the sales field is too long, the result is truncated and gives an inaccurate value.
You need a subquery that groups all the rows with the same price together, then a main query that performs the GROUP_CONCAT by code.
SELECT code, group_concat( CONCAT( quantity, 'x', FORMAT(price, 2 ))SEPARATOR '+' ) AS sales
FROM (
SELECT code, price, SUM(quantity) as quantity
FROM detalle_sales
GROUP by code, price) AS subq
GROUP BY code
DEMO
While I consider it a not-so-good idea to create this sort of format in the DB, here is how it goes:
SELECT code, group_concat( CONCAT( total, 'x', FORMAT(price, 2 ))SEPARATOR '+' ) AS sales
FROM (
SELECT
code, price,
SUM(quantity) AS total
FROM detalle_sales
GROUP BY code, price
) AS baseview
GROUP BY code