Sorting some rows by average with SQL
NickName:Jon Gjengset Ask DateTime:2011-07-15T01:49:44

Sorting some rows by average with SQL

All right, so here's a challenge for all you SQL pros: I have a table with two columns of interest, group and birthdate. Only some rows have a group assigned to them. I now want to print all rows sorted by birthdate, but I also want all rows with the same group to end up next to each other. The only semi-sensible way of doing this would be to use the groups' average birthdates for all the rows in the group when sorting. The question is, can this be done with pure SQL (MySQL in this instance), or will some scripting logic be required?

To illustrate, with the given table:

id | group | birthdate
---+-------+-----------
1  | 1     | 1989-12-07
2  | NULL  | 1990-03-14
3  | 1     | 1987-05-25
4  | NULL  | 1985-09-29
5  | NULL  | 1988-11-11

and let's say that the "average" of 1987-05-25 and 1989-12-07 is 1988-08-30 (this can be found by averaging the UNIX timestamp equivalents of the dates and then converting back to a date. This average doesn't have to be completely correct!). The output should then be:

id | group | birthdate  | [sort_by_birthdate]
---+-------+------------+--------------------
4  | NULL  | 1985-09-29 | 1985-09-29
3  | 1     | 1987-05-25 | 1988-08-30
1  | 1     | 1989-12-07 | 1988-08-30
5  | NULL  | 1988-11-11 | 1988-11-11
2  | NULL  | 1990-03-14 | 1990-03-14

Any ideas?

Cheers, Jon

Copyright Notice:Content Author:「Jon Gjengset」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6697686/sorting-some-rows-by-average-with-sql

More about “Sorting some rows by average with SQL” related questions

Sorting some rows by average with SQL

All right, so here's a challenge for all you SQL pros: I have a table with two columns of interest, group and birthdate. Only some rows have a group assigned to them. I now want to print all rows s...

Show Detail

Average across rows in SQL Server 2008

I have the following table Data Data1 Data2 YTD ------------------------- 1 2 3 2 3 4 3 3 6 In the YTD column I have to average the rows data. I ...

Show Detail

Sorting algorithm by average score

See example data input below. (MSSQL is target system) There are values 1-14 (as example, target range is 1-300aprox). Each value have its own score. The goal is to sort the values by average score.

Show Detail

Average of grouped rows in Sql Server

I have an Sql Server Table.. it's something like this: Id ...... Column1 ...... Column2 ```````````````````````````````` 1 ........ 1 ............. 34 2 ........ 1 ............. 44 3 ........

Show Detail

SQL Selecting average of just two rows

I am currently just finishing up an SQL Database project for school. I have used W3school, Google and read over several of the SQL average questions I want to write a select statement that will ...

Show Detail

SQL Select Statement For Calculating A Running Average Column

I am trying to have a running average column in the SELECT statement based on a column from the n previous rows in the same SELECT statement. The average I need is based on the n previous rows in the

Show Detail

Average of last n or 10 rows only in SQL Server

I have a result set from a query. Let's say it contains 100 rows, with 2 columns. I want a third computed column as the average of the last 10 rows. So like this: Hours Pay Last10Avg ------...

Show Detail

Using {Rows Preceding} for moving average sql

In the following code I'm trying to compute moving average of each month compared to the last 6 months,However SQL is excluding the rows which are null in the denominator for average. For Example:-

Show Detail

Get Average value for each X rows in SQL

Let`s say I have the following table +----+-------+ | Id | Value | +----+-------+ | 1 | 2.0 | | 2 | 8.0 | | 3 | 3.0 | | 4 | 9.0 | | 5 | 1.0 | | 6 | 4.0 | | 7 |

Show Detail

SQL average for fields with 0

I have a SQL queries with the some ratings like location, cleanliness, money, kitchen, checkinprocess, servicebyowner For few data checkinprocess, servicebyowner are 0. I just want to calculate the

Show Detail