Say I have a table users with columns user_id, name, rate. I want to get % positive rating. The formula for this is:
((all records with rating >= 3) / (total records)) * 100
This can be done by getting total records and all records with rating >= 3 by two queries. But I want to get this done using only one query. How can I do this?
select sum(rate >= 3) * 100 / count(*)
from users
Related
I really am struggling with a MySQL query, I have a table named 'info' and in it I have a column called 'rating' in it I have ratings between 1-10.
Now I need to generate a percentage value of how many ratings are from 1-6 and 7-8 and 9-10 but I need them to display desperately and after that I need a second query that can subtract the percentage value of the results from 1-6 and 9-10.
The query below is as close as I could get from all my research however I don't know how to get a percentage of ratings 1-6 only and not all of them and also how to get a second query to subtract the 1-6 and 9-10 rating percentages. Any help would be amazing.
SELECT rating,
COUNT(*) AS Count,
(COUNT(*) / _total ) * 100 AS Percentege
FROM info,
(SELECT COUNT(*) AS _total FROM info) AS myTotal
GROUP BY rating
Try this:
select if(rating between 1 and 6, '1-6',
if( rating between 7 and 8, '7-8',
'9-10' )
) as rating_range,
count(1) as num
from info
group by rating_range
fiddle code
I have this issue that is going around my head lately, I wanna know how many of my users can have more than a 50% of profit in their accounts. So, as you can see in the table, I have investment and earnings, so logically I must do a percentage for having the profit. Like: ((earnings - investment * 100) / investment)
ID Investment (euros) Earnings (euros)
13 2 3.4
14 1 0
15 4 7
16 12 20.76
From here everything is ok, but my question is, how can I calculate and show at the same time the users that have more than a 50% of profit?
I made this simple query that works but don't shows the users that have more than 50%, shows everyone.
select id, ((MAX(earnings)-investment)*100)/investment
from users
group by id, investment
;
So, my idea was to create a subquery, but I have a great issue with this:
select id
from users
where 50 < (
select (((MAX(earnings)-investment)*100)/investment)
from users
group by investment
)
group by id
;
I received this error:
Detail: Expected no more than one row to be returned by expression
I don't know what I'm doing wrong. Jalp!
Isn't this the formula?
select id, (sum(earnings) - sum(investment))*100 / sum(investment) as profit
from users
group by id;
Then if you want to filter, add:
having profit > 50
To get the count, use a subquery:
select count(*)
from (select id, (sum(earnings) - sum(investment))*100 / sum(investment) as profit
from users
group by id
) u
where profit > 50;
I have a table with 2 dates in it and a product, and I need to get the average days difference between them considering just the last 3 rows for each product.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121
This gives me the average of all the date differences for product 121
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121 LIMIT 3
Still gives me the average off all the records, ignoring the LIMIT argument.
Also when I try a different approach, it also does ignore the last argument and shows the average off all the rows.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product =121 && date1 > 2015-01-01
Any idea on how to fix this or what I'm doing wrong?
When you have problems like this, I recommend breaking it up and putting it back.
Before doing any calculations, you know that you need the last three rows for each product. So, if you want for example the rows with the latest date2 you can select them by doing the following:
SELECT *
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3;
That will select the 3 latest rows you want. Then, just use that as a subquery to preform the aggregation. This way, the calculations are only made on the rows you are concerned with:
SELECT product, AVG(DATEDIFF(date2, date1))
FROM(
SELECT product, date1, date2
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3) tempTable;
Just like what I asked in the title. For example, I have a table with ID and Total. I wanna list all the IDs and Totals if they satisfy that Total < 100. Otherwise, I want to group these rows with Total >= 100 in one row as ID is 000, and Total is the sum of those Totals.
May I know how I can achieve that?
Thank you.
You can use UNION
Select id, total
From TableA
Where total < 100
Union
Select 0 as id , sum(total) as total
From TableA
Where Total >=100
I have joined 3 tables in my query. In my Inventory db,Price is taken from table c and quantity is taken from table b. How can I show the records list of users who have ordered between the given value and maximum value of the column.
I am using below query in mysql to retrieve records. As expected it shows error. Any help will be highly appreciated
SELECT .... GROUP BY userid HAVING SUM(c.`price` * b.`quantity`) BETWEEN 9000 AND MAX(SUM(c.`price` * b.`quantity`))
If I understand correctly you don't need BETWEEN. Try it this way
SELECT ....
GROUP BY userid
HAVING SUM(c.`price` * b.`quantity`) >= 9000
In case you wondered you can't chain aggregate functions. And even if you could it wouldn't make sense because you group by userid, but trying to get MAX of SUM from all users. In order for this to work you should've used a subquery to get max value e.g.
SELECT ....
GROUP BY userid
HAVING SUM(c.`price` * b.`quantity`) =
(
SELECT MAX(total) total
FROM
(
SELECT SUM(c.`price` * b.`quantity`) total
GROUP BY userid
) q
)