Data compaction in (My)SQL - mysql

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

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

Finding the sum of values in a column for the last n rows

I'm trying to find the sum of values in a particular column for the last ten rows selected by some criteria and ordered by some column. I tried the obvious:
SELECT SUM(column) AS abc FROM table WHERE criteria ORDER BY column DESC LIMIT 10
However this seems to sum the entire column!?
So after playing around this seems to work:
SELECT SUM(column) AS abc FROM (SELECT column FROM table WHERE criteria ORDER BY column DESC LIMIT 10) AS abc
My questions...
Why doesn't the more intuitive approach work?
I could access the result by using $data[0], but I prefer to have some meaningful variable. So why do I need to do AS abc twice?
Is there a tidier/better way to do the job?
I'm quite inexperienced with SQL queries so I would really appreciate any help.
Because mysql runs query in the following order:
FROM->WHERE->GROUP BY->HAVING->ORDER BY->LIMIT.
So limit will be applied after grouping and will filter groups but not ordinary rows.
Regarding abs twice: it's necessary to add alias for all derived queries. This is mysql rule.

MySQL Average of column where there are greater than 5 rows

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

mysql, using group_concat

Could anyone let me know how to limit the number of values in GROUP_CONCAT for each group in MySQL? I am using the below query which also produces more than 2 concatenated values for each group
SELECT GROUP_CONCAT(remaining)
FROM `busroute`
GROUP BY bus
Could anyone let me know how to modify the above query for my problem?
I don't know of a way to limit the number of rows that are grouped, and I don't think that you can do it.
But if you are only going to want to have two rows that you want to group, you can do the grouping and group_concat manually and only group two rows at a time:
SELECT t1.bus, concat(t1.remaining, ',', t2.remaining)
FROM busroute as t1
JOIN busroute as t2 on t1.bus = t2.bus
WHERE t1.id < t2.id
Here we've just gotten two copies of the busroute table and then joined them together on the bus number, then we take some unique column value in the row (which could be any column as long as it's column as the unique attribute set on it) and eliminate matches of a row against its self. I used '<' rather than '<>' since I only want to match the same pair of non-unique rows once.

MySQL: Count two things in one query?

I have a "boolean" column in one of my tables (value is either 0 or 1).
I need to get two counts: The number of rows that have the boolean set to 0 and the number of rows that have it set to 1. Currently I have two queries: One to count the 1's and the other to count the 0's.
Is MySQL traversing the entire table when counting rows with a WHERE condition? I'm wondering if there's a single query that would allow two counters based on different conditions?
Or is there a way to get the total count along side the WHERE conditioned count? This would be enough as I'd only have to subtract one count from the other (due to the boolean nature of the column). There are no NULL values.
Thanks.
You could group your records by your boolean column and get count for each group.
SELECT bool_column, COUNT(bool_column) FROM your_table
WHERE your_conditions
GROUP BY bool_column
This will obviously work not only for bool columns but also with other data types if you need that.
Try this one:
SELECT
SUM(your_field) as positive_count,
SUM(IF(your_field, 0, 1)) as negative_count
FROM thetable
If they are all either 0 or 1 and you dont mind 2 rows as result you can group by that field and do a count like so:
select field, count(field)
from table
group by field
A simple group clause should do the trick :
SELECT boolField, COUNT(boolField)
FROM myTable
GROUP BY boolField