如何选择显示自特定谓词出现的最后一刻以来的天数的行?

时间:2015-04-10 07:55:01

标签: sql postgresql postgresql-9.4

我有一个表格,其数据如下所示:

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 AnalyticsAPI报告的一部分。 KEYWORD列包含用于宣传网站的关键字, CONVERSIONS列包含CREATED_AT中命名的每日转化次数。

特定谓词为date rangeWHERE '2015-04-01' <= created_at AND created_at <= '2015-04-09'

我尝试编写的查询应该打印自上次转化以来的天数和上次转化的日期(或者如果date range中没有转化,则为空/&#39;永远不会)。因此,我希望today(CURRENT_DATE =&#39; 2015-04-10&#39;)找到这样的结果:

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,但没有任何运气,我在这里没有什么可分享的,因为我甚至没有接近期望的结果。 : - (

2 个答案:

答案 0 :(得分:2)

您使用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;

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

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

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标准的语法,用于编写日期文字我喜欢它而不是隐式日期样式,只使用字符串。

SQLFiddle:http://sqlfiddle.com/#!15/d9aff/1

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

答案 1 :(得分:1)

这是我对此的处理方法(可能与数据库无关,但不要接受我的意见):

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