Answer the question
In order to leave comments, you need to log in
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.
Answer the question
In order to leave comments, you need to log in
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 ...
Please advise how to groupNo 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 datesFor 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]
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 questionAsk a Question
731 491 924 answers to any question