MySQL: Rank the total total amount with COUNT() - mysql

Edit: I want to rank with COUNT()
I've made a View with every order a customer made. In the next step I wrote a query to calculate the total amount a customer purchased.
Now I want to rank the customers, based on their total purchase.
I wrote this query:
SELECT u.m_name, SUM(u.num * u.price) AS total,
(SELECT COUNT(*)
FROM v_sales AS x
WHERE x.m_id = u.m_id
AND (SELECT SUM(s1.num * s1.price) FROM v_sales AS s1 WHERE s1.m_id = x.m_id)
>
(SELECT SUM(s2.num * s2.price) FROM v_sales AS s2 WHERE s2.m_id = x.m_id)
) + 1 AS Rank
FROM v_sales AS u
GROUP BY u.m_id;
But the results are not the expected ones:
# m_name total Rank
川島智弘 2620 1
河田英毅 0 1
山田忠明 15420 1
永峰弘万 500 1
永山智広 380 1
I need the following output:
# m_name total Rank
川島智弘 2620 2
河田英毅 0 5
山田忠明 15420 1
永峰弘万 500 3
永山智広 380 4
Has someone an idea what I did wrong? It would be also helpful if someone could explain why my query doesn't work.
Here is a Fiddle
Thank you

You can use the RANK function in MariaDB 10.4.
SELECT m_name, SUM(num * price) AS total,
RANK() OVER(ORDER BY SUM(num * price) DESC)
FROM v_sales
GROUP BY m_id;
Fiddle
No window function:
SELECT t1.m_name,MAX(t1.total),COUNT(t2.m_name)+1 as RANK
FROM
(SELECT m_name, SUM(num * price) AS total FROM
v_sales
GROUP BY m_id) t1
LEFT JOIN
(SELECT m_name, SUM(num * price) AS total FROM
v_sales
GROUP BY m_id) t2
ON t1.total<t2.total
GROUP BY t1.m_name
ORDER BY 3
Fiddle

Related

Output the total amount spent by the customer and the item on which he/she spent the most

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

get the most common value for each column

I'm attempting to create an SQL query that retrieves the total_cost for every row in a table. Alongside that, I also need to collect the most dominant value for both columnA and columnB, with their respective values.
For example, with the following table contents:
cost
columnA
columnB
target
250
Foo
Bar
XYZ
200
Foo
Bar
XYZ
150
Bar
Bar
ABC
250
Foo
Bar
ABC
The result would need to be:
total_cost
columnA_dominant
columnB_dominant
columnA_value
columnB_value
850
Foo
Bar
250
400
Now I can get as far as calculating the total cost - that's no issue. I can also get the most dominant value for columnA using this answer. But after this, I'm not sure how to also get the dominant value for columnB and the values too.
This is my current SQL:
SELECT
SUM(`cost`) AS `total_cost`,
COUNT(`columnA`) AS `columnA_dominant`
FROM `table`
GROUP BY `columnA_dominant`
ORDER BY `columnA_dominant` DESC
WHERE `target` = "ABC"
UPDATE: Thanks to #Barmar for the idea of using a subquery, I managed to get the dominant values for columnA and columnB:
SELECT
-- Retrieve total cost.
SUM(`cost`) AS `total_cost`,
-- Get dominant values.
(
SELECT `columnA`
FROM `table`
GROUP BY `columnA`
ORDER BY COUNT(*) DESC
LIMIT 1
) AS `columnA_dominant`,
(
SELECT `columnB`
FROM `table`
GROUP BY `columnB`
ORDER BY COUNT(*) DESC
LIMIT 1
) AS `columnB_dominant`
FROM `table`
WHERE `target` = "XYZ"
However, I'm still having issues figuring out how to calculate the respective values.
You might get close, if we want to get percentage values we can try to add COUNT(*) at subquery to get max count by columnA and columnB then do division by total count
SELECT
SUM(cost),
(
SELECT tt.columnA
FROM T tt
GROUP BY tt.columnA
ORDER BY COUNT(*) DESC
LIMIT 1
) AS columnA_dominant,
(
SELECT tt.columnB
FROM T tt
GROUP BY tt.columnB
ORDER BY COUNT(*) DESC
LIMIT 1
) AS columnB_dominant,
(
SELECT COUNT(*)
FROM T tt
GROUP BY tt.columnA
ORDER BY COUNT(*) DESC
LIMIT 1
) / COUNT(*) AS columnA_percentage,
(
SELECT COUNT(*)
FROM T tt
GROUP BY tt.columnB
ORDER BY COUNT(*) DESC
LIMIT 1
) / COUNT(*) AS columnB_percentage
FROM T t1
If your MySQL version supports the window function, there is another way which reduce table scan might get better performance than a correlated subquery
SELECT SUM(cost) OVER(),
FIRST_VALUE(columnA) OVER (ORDER BY counter1 DESC) columnA_dominant,
FIRST_VALUE(columnB) OVER (ORDER BY counter2 DESC) columnB_dominant,
FIRST_VALUE(counter1) OVER (ORDER BY counter1 DESC) / COUNT(*) OVER() columnA_percentage,
FIRST_VALUE(counter2) OVER (ORDER BY counter2 DESC) / COUNT(*) OVER() columnB_percentage
FROM (
SELECT *,
COUNT(*) OVER (PARTITION BY columnA) counter1,
COUNT(*) OVER (PARTITION BY columnB) counter2
FROM T
) t1
LIMIT 1
sqlfiddle
try this query
select sum(cost) as total_cost,p.columnA,q.columnB,p.columnA_percentage,q.columnB_percentage
from get_common,(
select top 1 columnA,columnA_percentage
from(
select columnA,count(columnA) as count_columnA,cast(count(columnA) as float)/(select count(columnA) from get_common) as columnA_percentage
from get_common
group by columnA)s
order by count_columnA desc
)p,
(select top 1 columnB,columnB_percentage
from (
select columnB,count(columnB) as count_columnB, cast(count(columnB) as float)/(select count(columnB) from get_common) as columnB_percentage
from get_common
group by columnB) t
order by count_columnB desc)q
group by p.columnA,q.columnB,p.columnA_percentage,q.columnB_percentage
so if you want to get the percent and dominant value you must make their own query like this
select top 1 columnA,columnA_percentage
from(
select columnA,count(columnA) as count_columnA,cast(count(columnA) as float)/(select count(columnA) from get_common) as columnA_percentage
from get_common
group by columnA)s
order by count_columnA desc
then you can join with the sum query to get all value you want
hope this can help you

Analysis of the completeness of the availability of products from the ClickHouse SQL database

Documentation: https://clickhouse.tech/docs/en/
Goal: 85% of the brand's product range should be available for purchase
Calculate the number of products of each brand by availability (maxItems > 0)
Make a breakdown of brands by the availability of the assortment to purchase:
Green: > 85%
Yellow: 70-84%
Red: 0-69%
Done:
SELECT brand, COUNT(1) AS cnt
FROM products
WHERE maxItems > 0
GROUP BY brand
ORDER BY cnt DESC;
Ok.
Below is an example to solve:
Each brand has an assortment, but it is not all available, but only some %.
You need to calculate this percentage for each brand using the formula:
(Number of all products (id) - The number of products that are not available (maxItems = 0)) / count(id) * 100% = result %
Below is an example of the request:
SELECT brand,
(((SELECT COUNT(1) FROM products) -
(SELECT COUNT(1) FROM products WHERE maxItems = 0)) /
(SELECT COUNT(1) FROM products) * 100) as cnt
FROM products
WHERE cnt > 85
GROUP BY brand
ORDER BY cnt DESC
LIMIT 1000;
Result:
brand cnt
Amorem 99.27102236131287
VALENS 99.27102236131287
FARFAL 99.27102236131287
VIAILA 99.27102236131287
4Kids 99.27102236131287
What should I fix in the code to count the percentage only by brand?
Thank you.
SELECT brand, (count() - countIf(maxItems = 0)) / count() * 100 as cnt
FROM products
GROUP BY brand
HAVING cnt > 85
ORDER BY cnt DESC
LIMIT 1000;

Get maximum value from two values

I have a table which gives the no of rides by a rider at each stand point. I need to find the stand for each rider for which he has the maximum rides.
My first result is in this format: 1
I require my final result like this: 2
I'm currently using this query, but I know it can be done in a better manner. Any suggestions would be helpful.
select c.rider_id, c.end_stand, b.max_rides
from
(select rider_id, max(rides) as max_rides
from
(select rider_id, end_stand, count(id) as rides
from ride where end_stand is not null
group by 1,2) a
group by 1
order by 2 desc, 1) b
join
(select rider_id, end_stand, count(id) as rides
from ride where end_stand is not null
group by 1,2) c
on c.rider_id = b.rider_id and c.rides = b.max_rides
order by 3 desc, 2,1
Before window functions, one method is a correlated subquery in the having clause:
select rider_id, end_stand, count(*) as rides
from ride r
where end_stand is not null
group by rider_id, end_stand
having count(*) = (select count(*)
from ride r2
where r2.end_stand is not null and
r2.rider_id = r.rider_id
group by r2.rider_id, r2.end_stand
order by count(*) desc
limit 1
);
With window functions, this is, of course, much simpler:
select *
from (select rider_id, end_stand, count(*) as rides
rank() over (partition by rider_id order by count(*) desc) as seqnum
from ride r
where end_stand is not null
group by rider_id, end_stand
) r
where seqnum = 1;
Both these will return duplicates, if there are ties for the max. The second version is easy to fix, if you want only one row: use row_number() instead of rank().

get top and bottom 25th percentile average

I have a table with list of employees and the number of units that they have sold.
I want to get the top 25 percentile Avg units sold and Bottom 25 percentile Avg units sold.
I have created a representation of my data SLQ Fiddle
I really have no idea how to start on this? All the examples i see are for SQL Server and not MySQL. Here is what i am thinking.
I want 25 percentile and cant limit to 25 items. Basically it would involve:
1) #_of_employees = The number of total employees.
2) #_of_employees_in_25_percentile = #_of_employees*0.25
3) Calculate the sum of the units sold by the top/bottom 25 percentile (limit #_of_employees_in_25_percentile)
4) Divide the sum by #_of_employees_in_25_percentile to get the average.
How can all this be done efficiently in MySQL?
This is a solution that uses a devious trick I learned from this question.
SELECT id, unit_sold, n * 100 / #total AS percentile
FROM (
SELECT id, unit_sold, #total := #total + unit_sold AS n
FROM mydata, (SELECT #total := 0) AS total
ORDER BY unit_sold ASC
) AS t
SQL Fiddle.
What about this?
SELECT
SUM(unit_sold) AS sum_tot, SUM(unit_sold)/count(id) AS average,
SUM(CASE WHEN percentile<25 THEN unit_sold ELSE 0 END) AS sum_top25,
SUM(CASE WHEN percentile<25 THEN 1 ELSE 0 END) AS count_top25,
SUM(CASE WHEN percentile<25 THEN unit_sold ELSE 0 END)/SUM(CASE WHEN percentile<25 THEN 1 ELSE 0 END) AS average_top25,
SUM(CASE WHEN percentile>75 THEN unit_sold ELSE 0 END) AS sum_bottom25,
SUM(CASE WHEN percentile>75 THEN 1 ELSE 0 END) AS count_bottom25,
SUM(CASE WHEN percentile>75 THEN unit_sold ELSE 0 END)/SUM(CASE WHEN percentile>75 THEN 1 ELSE 0 END) AS average_bottom25
FROM
(SELECT
id, unit_sold, c * 100 / #counter AS percentile
FROM
(SELECT
m.*, #counter:=#counter+1 AS c
FROM
(SELECT #counter:=0) AS initvar, mydata AS m
ORDER BY unit_sold desc
) AS t
WHERE
c <= (25/100 * #counter)
OR c >= (75/100 * #counter)
) AS t2
Output:
SUM_TOT AVERAGE SUM_TOP25 COUNT_TOP25 AVERAGE_TOP25 SUM_BOTTOM25 COUNT_BOTTOM25 AVERAGE_BOTTOM25
850 283.3333 500 1 500 350 2 175
See SQL Fiddle.
The idea is to use the MySQL: LIMIT by a percentage of the amount of records? solution to get the percentiles. Based on that (and on pdw answer) we create an output in which we just show the top 25% and bottom 75%.
Finally, we count and sum to get the values you requested.
Note this runs on top of the command:
SELECT
id, unit_sold, c * 100 / #counter AS percentile
FROM
(SELECT
m.*, #counter:=#counter+1 AS c
FROM
(SELECT #counter:=0) AS initvar, mydata AS m
ORDER BY unit_sold desc
) AS t
WHERE
c <= (25/100 * #counter)
OR c >= (75/100 * #counter)
Whose output is:
ID UNIT_SOLD PERCENTILE
d 500 20
a 250 80
e 100 100
How about going with this logic:
Select all, order by percentile (DESC), limit to 25
Select all, order by percentile (ASC), limit to 25
Is this the type of logic you're looking for ?
Sample queries:
$q1 = mysql_query(SELECT * FROM table_name ORDER BY percentile DESC LIMIT 25)
$q2 = mysql_query(SELECT * FROM table_name ORDER BY percentile ASC LIMIT 25)