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
Related
I have table like this
enter image description here
I need to get the data only whose age > 10, along with that i need to get the total number of records present in the table. ie. in this example it is 4 records. what i need is in single query i need to get the total number of records present in table and columns which i query.
Query will be somewhat like
SELECT ID, NAME, count(TOTAL NUMBER OF RECORDS IN TABLE) as Count from MYTABLE WHERE AGE > 10
Any idea about this ?
You can use a subquery in the FROM clause:
SELECT ID, NAME, c.cnt as Count
FROM MYTABLE CROSS JOIN
(SELECT COUNT(*) as cnt FROM MYTABLE) c
WHERE AGE > 10 ;
Both databases support window functions, but they are not really helpful here, because the count is not filtered in the same way as the outer query. If you do want the filter for both, then in the most recent versions you can do:
SELECT ID, NAME, COUNT(*) OVER () as cnt
FROM MYTABLE
WHERE AGE > 10 ;
You can try below - using scalar subquery
SELECT ID, NAME, age,(select count(*) from mytable WHERE AGE > 10) as Count
from MYTABLE
WHERE AGE > 10
I have these tables:
Fiddle of tables: http://sqlfiddle.com/#!9/18c65
With this query:
SELECT user_id AS user, round(AVG(cost_freight), 0) AS average FROM `freights` GROUP BY user ORDER BY average ASC LIMIT 10
i get average of freights(cost_freight) grouped by user (the values in bold is the authenticated user):
But now I need to get average of freights(cost_freight) of every month grouped by user, the result should be like this:
and this is where I have tried several ways but I can't get the desired result.
I would very much appreciate your help.
Thanks.
Use one column(MONTH(date)) into group by. Try this one,
SELECT user_id AS user,MONTH(date), round(AVG(cost_freight), 0) AS average FROM `freights` GROUP BY user,MONTH(date) ORDER BY average ASC LIMIT 10
I have a voting application that writes values to a mysql db table. It is a preference/weighted voting system so people choose a first option, second option, and third option. These all go into separate fields in the table. I'm looking for a way to write a query that will assign numerical values to the responses (3 for a first response, 2 for a second, 1 for a first) and then display the value with the summed score. I've been able to do this for total number of votes
select count(name) as votes,name
from (select 1st_option as name from votes
union all
select 2nd_option from votes
union all
select 3rd_option from votes) as tbl
group by name
having count(name) > 0
order by 1 desc;
but haven't quite figured out how to assign values to response in each column and then pull them together. Any help is much appreciated. Thanks!
You could do something like this:
select sum(score) as votes,name
from (select 1st_option as name, 3 as score from votes
union all
select 2nd_option as name, 2 as score from votes
union all
select 3rd_option as name, 1 as score from votes) as tbl
group by name;
I am trying to find the count of rows returned by below query:
SELECT Count(id) AS fcount,
user_id
FROM customer_followups
WHERE id > 0
AND Date(followup_date) >= Str_to_date("24-09-2012", "%d-%m-%y")
AND Date(followup_date) <= Str_to_date("24-09-2012", "%d-%m-%y")
GROUP BY user_id
ORDER BY user_id
It returns me 5 Rows.
However if I fire the query WITHOUT the count function I only get one record. See below:
SELECT id,
user_id
FROM customer_followups
WHERE id > 0
AND Date(followup_date) >= Str_to_date("24-09-2012", "%d-%m-%y")
AND Date(followup_date) <= Str_to_date("24-09-2012", "%d-%m-%y")
GROUP BY user_id
ORDER BY user_id
What could be the reason? ideally the count should return 1 ??
COUNT gives you the number of the records for a group which satisfies the condition.
The COUNT function is an aggregate function that simply counts all the items that
are in a group.
Whereas in your second query you just find the id and user_id for that group. Remember if the group has different user-id it will return any of them to find all the user_id you can use GROUP_CONCAT() i.e. GROUP_CONCAT(user_id)
I would like to determine two things from a single query:
Most prevalent column in a table
The amount of times such column was located upon querying the table
Example Table:
user_id some_field
1 data
2 data
1 data
The above would return user_id # 1 as being the most prevalent in the table, and it would return (2) for the total amount of times that it was located in the table.
I have done my research and I came across two types of queries.
GROUP BY user_id ORDER BY COUNT(*) DESC
SUM
The problem is that I can't figure out how to use these two queries in conjunction with one another. For example, consider the following query which successfully returns the most prevalent column.
$top_user = "SELECT user_id FROM table_name GROUP BY user_id ORDER BY COUNT(*) DESC";
The above query returns "1" based on the example table shown above. Now, I would like to be able to return "2" for the total amount of times the user_id (1) was found in the table.
Is this by any chance possible?
Thanks,
Evan
You can include count(*) in the SELECT list:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC;
If you want only the first one:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC LIMIT 1;