0

Let's say there is a schema:

|date|value|

DBMS is SQLite.

I want to get N groups and calculate AVG(value) for each of them.

Sample:

2020-01-01 10:00|2.0
2020-01-01 11:00|2.0
2020-01-01 12:00|3.0
2020-01-01 13:00|10.0
2020-01-01 14:00|2.0
2020-01-01 15:00|3.0
2020-01-01 16:00|11.0
2020-01-01 17:00|2.0
2020-01-01 18:00|3.0

Result (N=3):

2020-01-01 11:00|7.0/3
2020-01-01 14:00|15.0/3
2020-01-01 17:00|16.0/3

I need to use a windowing function, like NTILE, but it seems NTILE is not usable after GROUP BY. It can create buckets, but then how can I use these buckets for aggregation?

SELECT
   /*AVG(*/value/*)*/,
   NTILE (3) OVER (ORDER BY date) bucket
FROM
   test
/*GROUP BY bucket*/
/*GROUP BY NTILE (3) OVER (ORDER BY date) bucket*/

Also dropped the test data and this query into DBFiddle.

5
  • No, there is no way in SQL language to adjust the GROUP BY expression accordingly.
    – Andreas
    Jul 10, 2020 at 11:49
  • Is it also not possible to get an average for every N number of rows? I.e get the avg of every 5 rows from the table?
    – Daniel
    Jul 10, 2020 at 12:12
  • Edit your question with sample data and expected results to clarify.
    – forpas
    Jul 10, 2020 at 12:26
  • Also there is no date_format() function in SQLite. How does your code work?
    – forpas
    Jul 10, 2020 at 12:31
  • @forpas: I used strftime, but now I rephrased the question, as it seem there is no need for grouping by date if I can utilize windowing functions.
    – Daniel
    Jul 10, 2020 at 12:54

2 Answers 2

1

You can use NTILE() window function to create the groups and aggregate:

SELECT 
  DATETIME(MIN(DATE), ((STRFTIME('%s', MAX(DATE)) - STRFTIME('%s', MIN(DATE))) / 2) || ' second') date, 
  ROUND(AVG(value), 2) avg_value
FROM (
  SELECT *, NTILE(3) OVER (ORDER BY date) grp
  FROM test
) 
GROUP BY grp;

To change the number of rows in each bucket, you must change the number 3 inside the parentheses of NTILE().

See the demo.
Results:

| date                | avg_value |
| ------------------- | --------- |
| 2020-01-01 11:00:00 | 2.33      |
| 2020-01-01 14:00:00 | 5         |
| 2020-01-01 17:00:00 | 5.33      |
15
  • Actually I think I always need to use 2 instead of (3-1), that's the average of max-min dates.
    – Daniel
    Jul 10, 2020 at 13:27
  • @Daniel fine but there is 1 problem. For this case the number 3 is divides the rows in 3 groups each containing 3 rows. Your requirement is every N rows. So NTILE will nit work in any case. I will edit my answer.
    – forpas
    Jul 10, 2020 at 13:30
  • @Daniel I edited. The use of NTILE is wrong in your case because NTILE(N) divides the rows of the table in N groups, not necessarily containing N rows in each group. The correct way is with ROW_NUMBER().
    – forpas
    Jul 10, 2020 at 13:35
  • Wait, I think we are not on the same path now: NTILE is working beautifully, I need N groups. But calculating the average date (first column) seems wrong, I think it should be DATETIME(MIN(DATE), ((STRFTIME('%s', MAX(DATE)) - STRFTIME('%s', MIN(DATE))) / 2) || ' second').
    – Daniel
    Jul 10, 2020 at 13:53
  • If you need N groups then my previous code would work. But in your question you say * for every N rows*. Which is correct?
    – forpas
    Jul 10, 2020 at 13:58
1

I need to use a windowing function, like NTILE, but it seems NTILE is not usable after GROUP BY. It can create buckets, but then how can I use these buckets for aggregation?

You first use NTILE to assign bucket numbers in a subquery, then group by it in an outer query.

Using sub-query

SELECT bucket
     , AVG(value) AS avg_value
  FROM ( SELECT value
              , NTILE(3) OVER ( ORDER BY date ) AS bucket
           FROM test
       ) x
 GROUP BY bucket
 ORDER BY bucket

Using WITH clause

WITH x AS (
   SELECT date
        , value
        , NTILE(3) OVER ( ORDER BY date ) AS bucket
     FROM test
)
SELECT bucket
     , COUNT(*) AS bucket_size
     , MIN(date) AS from_date
     , MAX(date) AS to_date
     , MIN(value) AS min_value
     , AVG(value) AS avg_value
     , MAX(value) AS max_value
     , SUM(value) AS sum_value
  FROM x
 GROUP BY bucket
 ORDER BY bucket

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.