如何从排序表中找到找到的事件之前和之后 X 个样本的平均值?
在下面的示例中,我有 sales_list 表。我想按日期对表格进行排序,并找到销售“计数”从小于 100 到大于 100 时的事件。然后找到该事件之前 2 天和之后 2 天的平均利润。
sales_list
date count profit
12/1/2023 20 100
12/2/2023 50 280
12/3/2023 125 660
12/4/2023 165 850
12/5/2023 85 150
12/6/2023 150 710
12/7/2023 180 740
Expected output:
date count profit avg_prev2 avg_next2
12/3/2023 125 660 190 500
12/6/2023 150 710 500 740
在这里,我正在努力解决似乎不正确的 AVG(LAG) 语法。我得到:“Windows 函数可能不会出现在聚合函数内”。
任何帮助将不胜感激。
WITH top_sales AS (
SELECT
date,
count,
profit
ROW_NUMBER() OVER (ORDER BY date) AS row_num,
CASE WHEN (
LAG(count) OVER (ORDER BY date) < 100 AND count >= 100)
THEN 1 ELSE 0 END AS high_count
FROM
sales_data
)
SELECT
date,
count,
profit,
AVG(LAG(profit, 2) OVER (ORDER BY date)) AS avg_prev2,
AVG(LEAD(profit, 2) OVER (ORDER BY date)) AS avg_next2
FROM
top_sales
WHERE
high_count = 1
下面的查询首先使用
cte
将递增的行号与每个条目关联起来。然后,在 left join
和两个子查询中使用行号来搜索所需的前面和后面的信息:
with sales as (
select row_number() over (order by s.date) r, s.* from sales_list s
)
select s1.*, (select avg(s3.profit) from sales s3 where s3.r >= s1.r - 2 and s3.r < s1.r) avg_prev2,
(select avg(s3.profit) from sales s3 where s3.r <= s1.r + 2 and s3.r > s1.r) avg_next2
from sales s1 left join sales s2 on s1.r = s2.r + 1
where s2.count is not null and s2.count < 100 and s1.count > 100