mysql sum with format - mysql

I want to rating my property to restrict the user to rate it only once.
So i am saving rating points and user id in the table.
Now i want the total avg rating of that property.
I am using my query like this
SELECT SUM(rating_number) as rating_number, FORMAT((SUM(total_points) / SUM(rating_number),1) as average_rating FROM post_rating WHERE post_id = 9781 AND status = 1
But i am getting errors. How can i get that total average rating.
Thanks

Use this
SELECT SUM(rating_number) as rating_number,
FORMAT((SUM(total_points) / SUM(rating_number)),2) as average_rating
FROM post_rating WHERE post_id = 9781 AND status = 1

Related

How to count quantity of duplicate data?

I want to count data with unique ID buat same as pn.This is my sample data:
id_form id_purchase pn
PUR3-20190515022552 PUR-20190515022552 02N64073
PUR2-20190515022552 PUR-20190515022552 02N64073
PUR1-20190515022552 PUR-20190515022552 02N64073
This is my code :
SELECT COUNT(*) as Total FROM pur_supp WHERE pn = '02N64073' GROUP BY id_purchase
When I run the code, that total still 3 but I want that total is 1.
Try this
SELECT COUNT(DISTINCT id_purchase) as Total FROM pur_supp WHERE pn = '02N64073'

Operating Arithmetic Operations in SQL SELECT

I'm stuck on this problem :
Hello everybody (don't know why it does not appear on the first line...)
I have 2 tables :
-factures (invoices)
-commandes_clients (orders)
Each table contain unique id_client.
I'm trying to get and sort the average amount order of each client based on invoices amount : (total amount invoice for each client / number of orders of the same client)
Client 1 Average order amount 1254.21
Client 2 Average order amount 951.88
Client 3 Average order amount 891.11
...
I would like to sort by the average amount to get the client palmares
Table facture contains :
id_client, invoice_number, total amount
Table commandes_clients contains :
id_client
Thanks for your help
SELECT id_client,sum(total_amount)/count(invoice_number) as avg
FROM factures group by id_client order by avg;
Or:
SELECT id_client,sum(total_amount)/count(CC.id) as avg
FROM factures FA
INNER JOIN commandes_clients CC ON FA.id_client=CC.id_client
group by FA.id_client order by avg;
Try above query.

How to use SUM and COUNT mysql in one query

Not sure if this is possible but I have the following
SELECT SUM(rating) as rating FROM details WHERE client_id = '$id'
The rating column contains individual ratings of users (1,2,3,4,5) stars. I need to sum them all to be able to calculate the average, but I would also like to get how many stars of each delimiter that user got as well.
For instance
The user has 3 records and the ratings are (3,4,3). With a SUM of 10 out of 3 records, I get 3.3 average. But I would like as well
3 stars = 2
4 stars = 1
Is it possible to do this with one query?
If I got your question right, you can use AVG() function instead of SUM().
In the following query, it calculates the average rating of this client_id, also counts number of each star as required in question.
SELECT
AVG(rating) as rating,
COUNT(CASE WHEN rating=1 THEN 1 END) as star_1,
COUNT(CASE WHEN rating=2 THEN 1 END) as star_2,
COUNT(CASE WHEN rating=3 THEN 1 END) as star_3,
COUNT(CASE WHEN rating=4 THEN 1 END) as star_4,
COUNT(CASE WHEN rating=5 THEN 1 END) as star_5
FROM
details
WHERE
client_id = ID_HERE
GROUP BY
client_id
SELECT client_id ,sum(if(rating='1',1,0)) onestar,sum(if(rating='2',1,0)) twostar,sum(if(rating='3',1,0)) threestar,sum(if(rating='4',1,0)) fourstar,sum(if(rating='5',1,0)) fivestar, SUM(rating) sumrate,AVG(rating) as avgrate FROM details WHERE client_id = '$id'
or for all clients
SELECT client_id ,sum(if(rating='1',1,0)) onestar,sum(if(rating='2',1,0)) twostar,sum(if(rating='3',1,0)) threestar,sum(if(rating='4',1,0)) fourstar,sum(if(rating='5',1,0)) fivestar, SUM(rating) sumrate,AVG(rating) as avgrate FROM details WHERE group by client_id
It is a bit unclear what exactly you are after, but this might work:
SELECT rating, count(rating) FROM details WHERE client_id = '$id' GROUP BY rating;
Also, not sure how you are getting $id, but if you are getting it from the user, you should be using prepared statements rather than inserting it directly in your SQL code.
You don't need to sum your rating. I believe what you want is to key off the rating. I'd suggest this.
This will group every rating then tell you how many times this client is rated something that many times. This would also help you account for any instances where they made a decimal vote (i.e. 4.5 stars)
SELECT
concat(rating, ' Stars') as rating,
count(*) as count
FROM details
WHERE client_id = '$id'
Group by concat(rating, ' Stars');
I think this should do it. Also, you said you're summing them only to take the average. Why not use AVG() instead.
SELECT COUNT(rating), AVG(rating) as rating FROM details WHERE client_id = '$id' GROUP BY rating

Find users who have min and max rating?

I have a Ratings(User_ID, Rating) table, The minimum rating for user is 1 and the maximum rating is 12
My question is: How to get users, who have both ratings, 1 and 12 ?
If I correctly understand, you need this:
select user_id from t where
rating IN(1,12)
group by user_id
having count(distinct rating) = 2
If there no chance that same rating may repeated for same user, then you can use count(rating) instead.

Error in subquery

im stuck here and i don't know how to fix it.
I have a db table which has users ID, user grade and date when someone has voted for that user (3 fields).
Im trying to read the user that has the highest average grade for todays date, limited to one.
But the problem is that i want to read only users that have 5 or more votes.
My query looks like this, but im getting an error:
SELECT
idusers,
AVG(votes) AS Grade
FROM rank
WHERE (data = '{$dbDate}')
AND ((SELECT count(ID) + 1 FROM rank) AS tmpcount WHERE tmpcount>4)
GROUP BY idusers
ORDER BY Grade DESC
LIMIT 1
Without the tmpcount>4 clause this query is working ok, but I need to count the Id's.
You have to use HAVING to filter the result set on aggregated values such as COUNT (SUM, MIN, MAX, AVG, …):
SELECT idusers, AVG(votes) AS Grade
FROM rank
WHERE (data = '{$dbDate}')
GROUP BY idusers
HAVING COUNT(*) > 4
ORDER BY Grade DESC
LIMIT 1