I
I
Igor Kolontar2019-02-07 09:51:28
Oracle
Igor Kolontar, 2019-02-07 09:51:28

How to make an aggregation with an overlap window?

There is a data set. Simplified, it is a specific date and the value for that date.
I can not figure out how to calculate the data aggregation for each date in such a way that the grouping is not only by this date, but also by additional dates -14 days. Those. it turns out a certain grouping with a condition that expands this partition. At the same time, these windows can overlap, i.e. the same value can participate in the calculation of the aggregation for different dates.
See the illustration, I hope it will be clearer.
Please advise how to make grouping or count over partition by on such windows? Or where to read about it.5c5bd52da8b4f542396675.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vapaamies, 2019-02-07
@Kotsubid

If supported by your DBMS, you need to use rows between (I take according to your example):
I can be wrong. I have not used SQL at this level for a long time, I have already forgotten the details ...

K
Konstantin Tsvetkov, 2019-02-07
@tsklab

Please advise how to group
No way. You did not specify by what criteria the groups are selected (SQL does not understand pictures). Sum by group implies that records are grouped. If you need the participation of one record in several amounts, make a subquery.
there are lines not only of this date, but also several earlier dates
For each unique date, make a separate subquery.
SELECT DISTINCT [Table].[Date], 
  (SELECT Count(*) FROM [Table] AS [ts] 
      WHERE ts.[Date] BETWEEN [Table].[Date] AND DATEADD(d, -14, [Table].[Date]) 
            AND ts.[Check] = 1) AS CountCheck 
  FROM [Table]

A
Artem Cherepakhin, 2019-02-07
@AltZ

If I understand the task correctly, that is, in SQL window functions LAG and LEAD, google them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question