PostgreSQL中的日期添加函数

PostgreSQL中的日期添加函数,postgresql,date-arithmetic,Postgresql,Date Arithmetic,我目前在Microsoft SQL Server中有以下代码,用于获取连续两天查看的用户 WITH uservideoviewvideo (date, user_id) AS ( SELECT DISTINCT date, user_id FROM clickstream_videos WHERE event_name ='video_play' and user_id IS NOT NULL ) SELECT currentday.date AS date,

我目前在Microsoft SQL Server中有以下代码,用于获取连续两天查看的用户

WITH uservideoviewvideo (date, user_id) AS (
  SELECT  DISTINCT date, user_id 
  FROM clickstream_videos
  WHERE event_name ='video_play'  
    and user_id IS NOT NULL
) 
SELECT currentday.date AS date, 
       COUNT(currentday.user_id) AS users_view_videos, 
       COUNT(nextday.user_id) AS users_view_next_day 
FROM userviewvideo currentday
  LEFT JOIN userviewvideo nextday 
         ON currentday.user_id = nextday.user_id AND DATEADD(DAY, 1, 
currentday.date) = nextday.date
GROUP BY currentday.date

我正试图让DATEADD函数在PostgreSQL中工作,但我一直不知道如何让它工作。有什么建议吗?

我不认为PostgreSQL真的有DATEADD函数。相反,只要做:

+间隔“1天”

SQL Server:

在当前日期2012年11月21日的基础上增加1天 选择DATEADDday,1,GETDATE;2012-11-22 17:22:01.423

PostgreSQL:

在当前日期2012年11月21日的基础上增加1天 选择当前_日期+间隔“1天”;2012-11-22 17:22:01 选择当前_日期+1;2012-11-22 17:22:01

您可以使用date+1来执行与dateadd等效的操作,但我认为您的查询并不能完成您想要执行的操作

您应该使用窗口函数,而不是:

with plays as (
  select distinct date, user_id
    from clickstream_videos
   where event_name = 'video_play' 
     and user_id is not null
), nextdaywatch as (
  select date, user_id, 
         case
           when lead(date) over (partition by user_id
                                     order by date) = date + 1 then 1
           else 0
         end as user_view_next_day
    from plays
)
select date, 
       count(*) as users_view_videos,
       sum(user_view_next_day) as users_view_next_day
  from nextdaywatch
 group by date
 order by date;   
列日期定义为日期还是时间戳?如果是date,那么date+1将为您执行此操作。