MySQL Average of column where there are greater than 5 rows - mysql

Is there any way to use MySQL to only return an average if there are more than X rows?
I am currently using the following query:
SELECT round(AVG(a_points),1) as a from points where user_id=X
Can this be done in MySQL or do I have to do a row count first then execute this statement?
The table contains
user_id a_points b_points
So a user could have lots of b_points but only 4 a_points and I wouldn't want to average at that point.

Will it work for you ?
SELECT round(AVG(points),1) as a from points where user_id=X HAVING COUNT(*) >5

Related

Only returns 1 row?

Picture 1 is the table containing the data
Picture 2 is my command
Why is it only returning 1 row and not the same for all the subjectIDs?
How do I make it return a row for each subjectID?
Thanks
p.s Please keep things simple I need to do this with basic sql.
You are running an AVG command. This will aggregate the results, since an average has to be run on multiple rows of data.
If you want to have it grouped in a different way, you can do this with a GROUP BY clause. This will return a row for every distinct values for the column(s) specified in the GROUP BY clause, with the calculated average and so on.
It would look similar to the following:
SELECT subjectid, AVG(result)
FROM Results
GROUP BY subjectid

mysql count query gives 1

I mistakenly run query
SELECT count(*) table_name
to learn row count. It gives 1 as result. Do you know what is the meaning of this result "1"?
Thanks,
Simple.
The table contains 1 record.
Basically COUNT returns the number of records found on the table.
COUNT

Aggregate specific number of rows

I have a database with many rows and I would like to consecutively aggregate say 10 rows and calculate the average of one column. So row 1 to 10 will be average value no. one, row 11 to 20 will be average value no. two, etc.
Can this be done in MySql?
You'll need to GROUP BY FLOOR(something/10) to group each 10 rows. A primary autoincrement key without gaps would be best for this.
SELECT FLOOR(something/10) as start_id, AVG(yourAmount)
FROM yourTable
GROUP BY FLOOR(something/10)
I managed to solve the ordering issue by using aliases and group/order by those in one simple query.
SELECT FLOOR(id/10) AS id_tmp, id AS sorter, AVG(col_1) AS col_1_avg
FROM my_table
GROUP BY id_tmp
ORDER BY sorter DESC
I'm not sure why this works but in MySQL 5.0 it works anyway.

Data compaction in (My)SQL

I've got a table with 2 columns. The first one is the auto-increment one and the second holds some numeric value. I need to group rows in such a way: get N consecutive rows (using the auto-increment field), count average of corresponding numeric values and put this 1 new line to the other table. Can anyone help me with the GROUP BY statement?
no need for group by ?
SELECT floor(id / 5) cnt,avg(2ndcol) from <table> group by cnt;
This will get groups of 5 rows and return the average of 2ndcol. Example http://www.sqlize.com/y4mTuDF1Cy
avg() docs
edited as per comments

SQL how to count all rows up to a max value

I am having trouble counting the number of rows until it reaches a certain PK.
My PK is called id and I want to count all rows until i reach a specified id
I have tried using this query but it doesn't work probably becuase I am using a MySQL table
select max(count(*)) from news where id=18 group by id
I get this error
Invalid use of group function
select count(*) from news where id<=18
I would use the following:
select count(id) from news where id <= 18
This will be more efficient as you are only returning one column in a row as opposed to all of them.