I want to make a SQL query that shows me the article that generated most income. (in a shop)
Table = orderrader
rownumber articlenumber ordernumber price amount
1 11 1 8624 3
2 6 1 4794 2
3 17 1 8755 3
4 1 1 7803 1
5 16 1 8987 3
6 10 1 4575 3
7 4 1 8747 1
8 15 1 5439 3
9 11 2 8624 3
10 1 2 7803 1
Following sql statement will return only one articlenumber with max revenue.
Select articlenumber, sum(price*amount) as totalincome
from orderrader
group by articlenumber
order by sum(price*amount) desc LIMIT 1
SELECT articlenumber
FROM orderrader
WHERE (price * amount) = (SELECT MAX(price * amount) FROM orderrader)
This should do the trick, i checked it on my own database. It will give ONLY the one with the highest price*amount
SELECT articlenumber, SUM(price*amount) AS income
FROM table
GROUP BY articlenumber
ORDER BY income DESC
select articlenumber, sum(price*amount) as s from orderrader group by articlenumber order by s desc;
Related
Given a table orders
id
customer_id
created_at
1
1
2022-09-01
2
2
2022-09-02
3
1
2022-09-03
4
1
2022-09-04
5
2
2022-09-04
How do I produce a column that describes which number in the series for the customers the order is?
Example
id
customer_id
created_at
order number
1
1
2022-09-01
1
2
2
2022-09-02
1
3
1
2022-09-03
2
4
1
2022-09-04
3
5
2
2022-09-5
2
You can use a window function for that. With a cumulative count over a partition by customer id, you get exactly the order number you need:
select orders.*,
count(*) over (partition by customer_id order by id) order_number
from orders
order by id;
In MySQL 5.7 you could do this:
select customer_id,
(select count(*)
from orders
where customer_id = main.customer_id and id <= main.id)
from orders main;
Sample table
id
id_sequence
date
1
1
2022-06-27
2
1
2022-06-27
3
1
2022-06-27
4
2
2022-06-27
5
2
2022-06-27
6
1
2022-06-28
7
1
2022-06-28
8
2
2022-06-28
9
2
2022-06-28
Expected Output
id
id_sequence
date
3
1
2022-06-27
5
2
2022-06-27
7
1
2022-06-28
9
2
2022-06-28
how can I make a query to get latest data on every date in MySql. tried to use MAX(id) for the id_sequence but it does not return a correct value since the expected output will take only highest id of every sequence and the output will only display distinct data of id_sequence 1,2 at date 2022-06-28.
If you want to make sure dates are taken distinctively, you need to add it inside the GROUP BY clause.
SELECT MAX(id) AS id,
id_sequence,
date_
FROM tab
GROUP BY id_sequence,
date_
ORDER BY id
Check the demo here.
There are multiple lineitem ids and I want to return the row with the largest quantity for each lineitem id. What is the best way to do this in Mysql?
Row completed_at lineitem_id quantity
1 2020-03-12 4453468635184 1
2 2020-03-06 4453468635184 2
3 2020-03-17 4480921108528 4
4 2020-03-25 4480921108528 2
5 2020-03-25 4481446608944 3
6 2020-03-17 4481446608944 5
With mysql you can write like this:
SELECT *
FROM (
SELECT *
FROM your_table
ORDER BY lineitem_id, quantity DESC
LIMIT 9999999999999 -- for mariadb, support order in subquery
) AS your_alias
GROUP BY lineitem_id
Currently I am honestly at loss what I am doing wrong. It is a rather simple query I think.
Tables:
operations:
id processedon clientid
1 2018-01-01 9
2 2018-03-16 9
3 2018-04-21 9
4 2018-04-20 9
5 2018-05-09 9
items:
id operation_id quantity unitprice
1 1 10 2
2 1 5 3
3 2 20 4
4 3 10 2
5 4 8 4
6 4 10 4
7 5 2 2
The expected result of the operation/query is:
month total_value
1 35
3 80
4 92
5 4
That is quantity * unitprice based. For some reason, it only returns month=4
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON (`items`.`operation_id` = `operations`.`id`)
GROUP BY 'month'
ORDER BY 'month'
According to the info provided the join should be
INNER JOIN operations ON items.operation_id = operations.id
Eg
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON `items`.`operation_id` = `operations`.`id`
GROUP BY month(`operations`.`processedon`)
ORDER BY `month`
There is no efficiency gain by using a column alias in the group by clause, I prefer to avoid using them except perhaps in the order by clause.
The following query will give you the required answer
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM items
INNER JOIN operations ON (items.operation_id = operations.id)
GROUP BY month(operations.processedon)
ORDER BY month(operations.processedon)
You need to specify month correctly since it is not an existing column.
You'll get the following result
month total_value
1 35
3 80
4 92
5 4
I have a schema like the following
id (INT)
Cycle_Number (INT)
Cycle_Day (INT)
Date (date)
...other columns irrelevant to the question...
How can I get the row that has the max Cycle_Day within the max Cycle_Number
For example, say I have the following data
ID Cycle_Number Cycle_Day Date
1 1 1 2011-12-01
2 1 2 2011-12-02
3 2 1 2011-12-03
4 2 2 2011-12-04
5 2 3 2011-12-05
6 2 4 2011-12-06
7 3 1 2011-12-07
8 3 2 2011-12-08
9 3 3 2011-12-09
The query would return row 9. (It has the highest Cycle_Day within the highest Cycle_Number)
Thanks
this one is compatible MySql 5.5 with no joint tables
SELECT id
FROM cycles
ORDER BY Cycle_Number DESC , Cycle_Day DESC
LIMIT 0 , 1
Regards
This SQL query should provide the max value you want.
SELECT ID, Cycle_Number, Cycle_Day, Date
FROM yourTable AS t
CROSS JOIN (
SELECT MAX(Cycle_Number) AS Cycle_Number FROM yourTable
) AS sq USING (Cycle_Number)
ORDER BY Cycle_Day DESC LIMIT 1