如何 select 行显示自上次出现特定谓词以来的天数?

How to select rows displaying number of days since last moment of occurrence of specific predicate?

我有一个 table 数据如下所示:

KEYWORD, CONVERSIONS, CREATED_AT
'Travel agent', 0, '2015-04-01'
'Travel agent', 0, '2015-04-02'
'Travel agent', 0, '2015-04-03'
'Travel agent', 0, '2015-04-04'
'Travel agent', 0, '2015-04-05'
'Travel agent', 0, '2015-04-06'
'Travel agent', 0, '2015-04-07'
'Travel agent', 0, '2015-04-08'
'Travel agent', 0, '2015-04-09'
'Travel agency vizier', 0, '2015-04-01'
'Travel agency vizier', 0, '2015-04-02'
'Travel agency vizier', 0, '2015-04-03'
'Travel agency vizier', 0, '2015-04-04'
'Travel agency vizier', 0, '2015-04-05'
'Travel agency vizier', 0, '2015-04-06'
'Travel agency vizier', 0, '2015-04-07'
'Travel agency vizier', 0, '2015-04-08'
'Travel agency vizier', 1, '2015-04-09'
'Coral', 0, '2015-04-01'
'Coral', 0, '2015-04-02'
'Coral', 0, '2015-04-03'
'Coral', 0, '2015-04-04'
'Coral', 0, '2015-04-05'
'Coral', 0, '2015-04-06'
'Coral', 0, '2015-04-07'
'Coral', 0, '2015-04-08'
'Coral', 0, '2015-04-09'
'Coral travel', 0, '2015-04-01'
'Coral travel', 0, '2015-04-02'
'Coral travel', 0, '2015-04-03'
'Coral travel', 0, '2015-04-04'
'Coral travel', 0, '2015-04-05'
'Coral travel', 0, '2015-04-06'
'Coral travel', 0, '2015-04-07'
'Coral travel', 0, '2015-04-08'
'Coral travel', 0, '2015-04-09'
'Egypt last minute', 1, '2015-04-01'
'Egypt last minute', 1, '2015-04-02'
'Egypt last minute', 0, '2015-04-03'
'Egypt last minute', 0, '2015-04-04'
'Egypt last minute', 0, '2015-04-05'
'Egypt last minute', 0, '2015-04-06'
'Egypt last minute', 0, '2015-04-07'
'Egypt last minute', 0, '2015-04-08'
'Egypt last minute', 0, '2015-04-09'
'Egypt holidays', 0, '2015-04-01'
'Egypt holidays', 0, '2015-04-02'
'Egypt holidays', 0, '2015-04-03'
'Egypt holidays', 0, '2015-04-04'
'Egypt holidays', 0, '2015-04-05'
'Egypt holidays', 0, '2015-04-06'
'Egypt holidays', 0, '2015-04-07'
'Egypt holidays', 0, '2015-04-08'
'Egypt holidays', 0, '2015-04-09'
'Exim', 0, '2015-04-01'
'Exim', 0, '2015-04-02'
'Exim', 0, '2015-04-03'
'Exim', 0, '2015-04-04'
'Exim', 0, '2015-04-05'
'Exim', 0, '2015-04-06'
'Exim', 0, '2015-04-07'
'Exim', 0, '2015-04-08'
'Exim', 0, '2015-04-09'
'Greece last minute', 0, '2015-04-03'
'Greece last minute', 0, '2015-04-04'
'Greece last minute', 0, '2015-04-05'
'Greece last minute', 0, '2015-04-06'
'Greece last minute', 0, '2015-04-07'
'Greece last minute', 0, '2015-04-08'
'Greece last minute', 0, '2015-04-09'
'Greece holidays', 0, '2015-04-01'
'Greece holidays', 0, '2015-04-02'
'Greece holidays', 0, '2015-04-03'
'Greece holidays', 0, '2015-04-04'
'Greece holidays', 0, '2015-04-05'
'Greece holidays', 0, '2015-04-06'
'Greece holidays', 0, '2015-04-07'
'Greece holidays', 0, '2015-04-08'
'Greece holidays', 0, '2015-04-09'
'Last-minute', 0, '2015-04-01'
'Last-minute', 0, '2015-04-02'
'Last-minute', 0, '2015-04-03'
'Last-minute', 0, '2015-04-04'
'Last-minute', 0, '2015-04-05'
'Last-minute', 0, '2015-04-06'
'Last-minute', 0, '2015-04-07'
'Last-minute', 0, '2015-04-08'
'Last-minute', 0, '2015-04-09'
'Last Minute', 1, '2015-04-01'
'Last Minute', 1, '2015-04-02'
'Last Minute', 0, '2015-04-03'
'Last Minute', 0, '2015-04-04'
'Last Minute', 0, '2015-04-05'
'Last Minute', 0, '2015-04-06'
'Last Minute', 0, '2015-04-07'
'Last Minute', 0, '2015-04-08'
'Last Minute', 0, '2015-04-09'
'Last Minute Egypt', 0, '2015-04-01'
'Last Minute Egypt', 0, '2015-04-02'
'Last Minute Egypt', 0, '2015-04-03'
'Last Minute Egypt', 0, '2015-04-04'
'Last Minute Egypt', 0, '2015-04-05'
'Last Minute Egypt', 0, '2015-04-06'
'Last Minute Egypt', 0, '2015-04-07'
'Last Minute Egypt', 0, '2015-04-08'
'Last Minute Egypt', 0, '2015-04-09'
'Last minute holidays', 0, '2015-04-01'
'Last minute holidays', 0, '2015-04-02'
'Last minute holidays', 0, '2015-04-03'
'Last minute holidays', 0, '2015-04-04'
'Last minute holidays', 0, '2015-04-05'
'Last minute holidays', 0, '2015-04-06'
'Last minute holidays', 0, '2015-04-07'
'Last minute holidays', 0, '2015-04-08'
'Last minute holidays', 0, '2015-04-09'
'Offer holiday', 0, '2015-04-01'
'Offer holiday', 0, '2015-04-02'
'Offer holiday', 0, '2015-04-03'
'Offer holiday', 0, '2015-04-04'
'Offer holiday', 0, '2015-04-05'
'Offer holiday', 0, '2015-04-06'
'Offer holiday', 0, '2015-04-07'
'Offer holiday', 0, '2015-04-08'
'Offer holiday', 0, '2015-04-09'
'Sun & fun', 0, '2015-04-01'
'Sun & fun', 0, '2015-04-02'
'Sun & fun', 0, '2015-04-03'
'Sun & fun', 0, '2015-04-04'
'Sun & fun', 0, '2015-04-05'
'Sun & fun', 0, '2015-04-06'
'Sun & fun', 0, '2015-04-07'
'Sun & fun', 0, '2015-04-08'
'Sun & fun', 2, '2015-04-09'
'Sun and fun', 0, '2015-04-01'
'Sun and fun', 0, '2015-04-02'
'Sun and fun', 0, '2015-04-03'
'Sun and fun', 0, '2015-04-04'
'Sun and fun', 0, '2015-04-05'
'Sun and fun', 0, '2015-04-06'
'Sun and fun', 0, '2015-04-07'
'Sun and fun', 0, '2015-04-08'
'Sun and fun', 1, '2015-04-09'
'Lodge', 0, '2015-04-01'
'Lodge', 0, '2015-04-02'
'Lodge', 0, '2015-04-03'
'Lodge', 0, '2015-04-04'
'Lodge', 0, '2015-04-05'
'Lodge', 1, '2015-04-06'
'Lodge', 1, '2015-04-07'
'Lodge', 1, '2015-04-08'
'Lodge', 0, '2015-04-09'
'Holiday greece', 0, '2015-04-01'
'Holiday greece', 0, '2015-04-02'
'Holiday greece', 0, '2015-04-03'
'Holiday greece', 0, '2015-04-04'
'Holiday greece', 0, '2015-04-05'
'Holiday greece', 0, '2015-04-06'
'Holiday greece', 0, '2015-04-07'
'Holiday greece', 0, '2015-04-08'
'Holiday greece', 0, '2015-04-09'
'Holiday', 4, '2015-04-01'
'Holiday', 4, '2015-04-02'
'Holiday', 1, '2015-04-03'
'Holiday', 1, '2015-04-04'
'Holiday', 1, '2015-04-05'
'Holiday', 3, '2015-04-06'
'Holiday', 3, '2015-04-07'
'Holiday', 3, '2015-04-08'
'Holiday', 3, '2015-04-09'
'Wakacje.pl', 13, '2015-04-01'
'Wakacje.pl', 13, '2015-04-02'
'Wakacje.pl', 1, '2015-04-03'
'Wakacje.pl', 1, '2015-04-04'
'Wakacje.pl', 1, '2015-04-05'
'Wakacje.pl', 6, '2015-04-06'
'Wakacje.pl', 6, '2015-04-07'
'Wakacje.pl', 6, '2015-04-08'
'Wakacje.pl', 4, '2015-04-09'
'Holiday 2014', 0, '2015-04-01'
'Holiday 2014', 0, '2015-04-02'
'Holiday greece', 1, '2015-04-01'
'Holiday greece', 1, '2015-04-02'
'Holiday greece', 0, '2015-04-03'
'Holiday greece', 0, '2015-04-04'
'Holiday greece', 0, '2015-04-05'
'Holiday greece', 0, '2015-04-06'
'Holiday greece', 0, '2015-04-07'
'Holiday greece', 0, '2015-04-08'
'Holiday greece', 0, '2015-04-09'
'Last-minute holiday', 0, '2015-04-01'
'Last-minute holiday', 0, '2015-04-02'
'Last-minute holiday', 0, '2015-04-03'
'Last-minute holiday', 0, '2015-04-04'
'Last-minute holiday', 0, '2015-04-05'
'Last-minute holiday', 0, '2015-04-06'
'Last-minute holiday', 0, '2015-04-07'
'Last-minute holiday', 0, '2015-04-08'
'Last-minute holiday', 1, '2015-04-09'
'Holiday last minute', 0, '2015-04-01'
'Holiday last minute', 0, '2015-04-02'
'Holiday last minute', 0, '2015-04-03'
'Holiday last minute', 0, '2015-04-04'
'Holiday last minute', 0, '2015-04-05'
'Holiday last minute', 0, '2015-04-06'
'Holiday last minute', 0, '2015-04-07'
'Holiday last minute', 0, '2015-04-08'
'Holiday last minute', 0, '2015-04-09'
'Vacation en', 0, '2015-04-01'
'Vacation en', 0, '2015-04-02'
'Vacation en', 0, '2015-04-03'
'Vacation en', 0, '2015-04-04'
'Vacation en', 0, '2015-04-05'
'Vacation en', 0, '2015-04-06'
'Vacation en', 0, '2015-04-07'
'Vacation en', 0, '2015-04-08'
'Vacation en', 0, '2015-04-09'
'Holiday in Greece', 0, '2015-04-01'
'Holiday in Greece', 0, '2015-04-02'
'Holiday in Greece', 0, '2015-04-03'
'Holiday in Greece', 0, '2015-04-04'
'Holiday in Greece', 0, '2015-04-05'
'Holiday in Greece', 0, '2015-04-06'
'Holiday in Greece', 0, '2015-04-07'
'Holiday in Greece', 0, '2015-04-08'
'Holiday in Greece', 1, '2015-04-09'
'Holidays abroad', 0, '2015-04-01'
'Holidays abroad', 0, '2015-04-02'
'Holidays abroad', 0, '2015-04-03'
'Holidays abroad', 0, '2015-04-04'
'Holidays abroad', 0, '2015-04-05'
'Holidays abroad', 0, '2015-04-06'
'Holidays abroad', 0, '2015-04-07'
'Holidays abroad', 0, '2015-04-08'
'Holidays abroad', 0, '2015-04-09'
'Holiday', 0, '2015-04-01'
'Holiday', 0, '2015-04-02'
'Holiday', 1, '2015-04-03'
'Holiday', 1, '2015-04-04'
'Holiday', 1, '2015-04-05'
'Holiday', 0, '2015-04-06'
'Holiday', 0, '2015-04-07'
'Holiday', 0, '2015-04-08'
'Holiday', 0, '2015-04-09'
'Egypt holiday last minute', 0, '2015-04-01'
'Egypt holiday last minute', 0, '2015-04-02'
'Egypt holiday last minute', 0, '2015-04-03'
'Egypt holiday last minute', 0, '2015-04-04'
'Egypt holiday last minute', 0, '2015-04-05'
'Egypt holiday last minute', 0, '2015-04-06'
'Egypt holiday last minute', 0, '2015-04-07'
'Egypt holiday last minute', 0, '2015-04-08'
'Egypt holiday last minute', 0, '2015-04-09'
'Holiday greece', 0, '2015-04-01'
'Holiday greece', 0, '2015-04-02'
'Holiday greece', 0, '2015-04-03'
'Holiday greece', 0, '2015-04-04'
'Holiday greece', 0, '2015-04-05'
'Holiday greece', 0, '2015-04-06'
'Holiday greece', 0, '2015-04-07'
'Holiday greece', 0, '2015-04-08'
'Holiday greece', 0, '2015-04-09'
'Travel abroad', 0, '2015-04-01'
'Travel abroad', 0, '2015-04-02'
'Travel abroad', 0, '2015-04-03'
'Travel abroad', 0, '2015-04-04'
'Travel abroad', 0, '2015-04-05'
'Travel abroad', 1, '2015-04-06'
'Travel abroad', 1, '2015-04-07'
'Travel abroad', 1, '2015-04-08'
'Travel abroad', 0, '2015-04-09'
'Vizier holidays', 1, '2015-04-01'
'Vizier holidays', 1, '2015-04-02'
'Vizier holidays', 0, '2015-04-03'
'Vizier holidays', 0, '2015-04-04'
'Vizier holidays', 0, '2015-04-05'
'Vizier holidays', 0, '2015-04-06'
'Vizier holidays', 0, '2015-04-07'
'Vizier holidays', 0, '2015-04-08'
'Vizier holidays', 0, '2015-04-09'
'Www vacation en ', 0,' 2015-04-01 '
'Www vacation en ', 0,' 2015-04-02 '
'Www vacation en ', 0,' 2015-04-03 '
'Www vacation en ', 0,' 2015-04-04 '
'Www vacation en ', 0,' 2015-04-05 '
'Www vacation en ', 0,' 2015-04-06 '
'Www vacation en ', 0,' 2015-04-07 '
'Www vacation en ', 0,' 2015-04-08 '
'Www vacation en ', 1,' 2015-04-09 '
'Trips', 0, '2015-04-01'
'Trips', 0, '2015-04-02'
'Trips', 0, '2015-04-03'
'Trips', 0, '2015-04-04'
'Trips', 0, '2015-04-05'
'Trips', 0, '2015-04-06'
'Trips', 0, '2015-04-07'
'Trips', 0, '2015-04-08'
'Trips', 0, '2015-04-09'
'Tours', 0, '2015-04-01'
'Tours', 0, '2015-04-02'
'Tours', 0, '2015-04-03'
'Tours', 0, '2015-04-04'
'Tours', 0, '2015-04-05'
'Tours', 0, '2015-04-06'
'Tours', 0, '2015-04-07'
'Tours', 0, '2015-04-08'
'Tours', 0, '2015-04-09'
'Trips abroad', 0, '2015-04-01'
'Trips abroad', 0, '2015-04-02'
'Trips abroad', 0, '2015-04-03'
'Trips abroad', 0, '2015-04-04'
'Trips abroad', 0, '2015-04-05'
'Trips abroad', 0, '2015-04-06'
'Trips abroad', 0, '2015-04-07'
'Trips abroad', 0, '2015-04-08'
'Trips abroad', 0, '2015-04-09'

它是 Google 分析 API 报告的一部分。 KEYWORD 列包含用于宣传网站的关键字, CONVERSIONS 列在 CREATED_AT.

指定日期的转化次数

具体谓词是date rangeWHERE '2015-04-01' <= created_at AND created_at <= '2015-04-09'

我尝试编写的查询应打印自上次转换以来的天数和上次转换的日期(如果 date range 中没有转换,则为 null/'never')。所以我正在寻找的结果看起来像这样 today (CURRENT_DATE = '2015-04-10'):

KEYWORD, DAYS_SINCE_CONVERSION, LAST_CONVERSION_DATE
'Travel agent', null, null
'Travel agency vizier', 1, '2015-04-09'
'Egypt last minute', 8, '2015-04-02'
'Exim', null, null
'Last Minute', 8, '2015-04-02'
'Sun & fun', 1, '2015-04-09'
... (and so on - I gave here few keywords just for example)

我尝试了一些 SQL,但没有任何运气,我没有什么可以在这里分享的,因为我什至没有接近期望的结果。 :-(

您用 postgresql-9.4 标记了您的问题,因此您可以为此使用过滤聚合:

select keyword, 
       max(created_at) filter (WHERE conversions > 0 AND created_at between '2015-04-01' AND '2015-04-09') as last_conversion_date
from conversion
group by keyword;

这将 return 每个关键字和给定时间范围内的最新 created_at 日期占一行,但前提是存在转化(必须过滤掉,例如 'Travel agent', 0, '2015-04-09'

要计算从那时起的天数,只需从 current_date 中减去最晚的日期。为了不重复表达式,将查询放入派生的 table 中会更容易:

select keyword, 
       current_date - last_conversion_date as days_since_conversion, 
       last_conversion_date
from (
  select keyword, 
         max(created_at) filter (WHERE conversions > 0 AND created_at between '2015-04-01' AND '2015-04-09') as last_conversion_date
  from conversion
  group by keyword
) t
order by keyword;

date '2015-04-01' 是 SQL 标准用于编写日期文字的语法我更喜欢它而不是仅使用字符串的隐式日期样式。

SQL在这里小提琴:http://sqlfiddle.com/#!15/d9aff/1

SQLFiddle 使用 case when 表达式而不是 filter 表达式,因为 filter 在早期的 Postgres 版本中不可用。

这是我对此的处理方法(可能与数据库无关,但不要相信我的话):

select c.keyword, t.days_since, t.last_date from conversation c
left join (
  select keyword, current_date - max(created_at) as days_since, max(created_at) as last_date
  from conversation
  where created_at between '2015-04-01' and '2015-04-09'
  and conversations > 0
  group by keyword
) t on t.keyword = c.keyword
group c.keyword, t.days_since, t.last_date