如何将当前行与Redshift中下一行的聚合进行比较?

时间:2017-06-15 23:39:05

标签: sql sql-server amazon-redshift

我想使用逻辑比较(lead(),lag(),window)在单个SQL查询中检索结果,以将行值与接下来n行的聚合进行比较。

例如, 我有一个具有以下属性的图书数据库:

  

id |名字| date_issued |键入
  1 | abc | 2017-05-01 |本书
  2 | def | 2017-05-01 |本书
  3 | xyz | 2017-05-01 |杂志
  4 | abc | 2017-04-30 |本书
  5 | def | 2017-04-29 |书

所以,我想将一天发行的一些书籍与未来3天发行的一些书籍的平均值进行比较。

2 个答案:

答案 0 :(得分:1)

对于您的具体问题,您似乎不需要行数,但需要一个时间间隔,因为有时每天可能有1条记录,有时甚至更多。您需要每天获得N本书,然后以范围条件自行加入。查询将如下所示(尽管尚未测试):

with
books_by_day as (
    select date_issued as date, count(1) as books
    from books
    group by 1
)
select 
 t1.date
,t1.books as books_at_that_date
,avg(1.0*t2.books) as books_at_next_3_days
from books_by_day t1
left join books_by_day t2
on t2.date between t1.date+interval '1 day' and t1.date+interval '3 days'
group by 1,2
order by 1;

答案 1 :(得分:0)

以下是如何使用select语句中的子查询获取所需信息的示例:

DECLARE @Books TABLE (Name VARCHAR(255), Date_Issued DATE);
INSERT @Books VALUES ('ABC', '2017-05-01'), ('DEF', '2017-05-01'), ('XYZ', '2017-05-01'), ('ABC', '2017-04-30'), ('DEF', '2017-04-29');

SELECT *,
       SoldToday = COUNT(*) OVER (PARTITION BY Date_Issued),
       NextThreeDaysAverage = 
        (
            SELECT AVG(BookCount * 1.0)
            FROM
            (
                SELECT BookCount = COUNT(*)
                FROM @Books AS B2
                WHERE B2.Date_Issued > B.Date_Issued
                AND B2.Date_Issued < DATEADD(DAY, 4, B.Date_Issued)
                GROUP BY B2.Date_Issued
            ) AS T
        )
FROM @Books AS B;