Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
高效的SQL查询,提供每个月至少登录一次的用户列表_Sql_Loops_Repeat - Fatal编程技术网

高效的SQL查询,提供每个月至少登录一次的用户列表

高效的SQL查询,提供每个月至少登录一次的用户列表,sql,loops,repeat,Sql,Loops,Repeat,我需要一个有效的方法(如果可能的话)给我一个每个月至少登录一次的用户列表 该表称为login。该ID是用户\u ID 我需要一个用户的名单,至少每月登录一次,在过去的36个月! 我可以像这样做一长串嵌套子查询 select distinct user_id from login where extract (year from timestamp::date) = 2017 and extract (month from timestamp::date) = 1 and user_id in

我需要一个有效的方法(如果可能的话)给我一个每个月至少登录一次的用户列表

该表称为login。该ID是用户\u ID

我需要一个用户的名单,至少每月登录一次,在过去的36个月! 我可以像这样做一长串嵌套子查询

select distinct user_id 
from login 
where extract (year from timestamp::date) = 2017
and extract (month from timestamp::date) = 1
and user_id in (
select user_id 
from login 
where extract (year from timestamp::date) = 2017
and extract (month from timestamp::date) = 2
and user_id in (
select user_id 
from login 
where extract (year from timestamp::date) = 2017
and extract (month from timestamp::date) = 3
and user_id in (..........
.........
我在亚马逊红移


谢谢

此逻辑应该有效:每个用户/月组合获得一行,然后计算这些行:

select user_id
from
 ( select user_id --one row per user/month
   from login
   -- the last 36 months
   where timestamp >= timestamp '2015-02-01 00:00:00' 
   group by 
      user_id 
     ,extract (year from timestamp::date)
     ,extract (month from timestamp::date)
 ) as dt
group by user_id
having count(*) = 36 -- one row per month
或者使用红移函数将时间戳截断为月1日,然后计数(不同):


此逻辑应该有效:每个用户/月组合获取一行,并计算这些行:

select user_id
from
 ( select user_id --one row per user/month
   from login
   -- the last 36 months
   where timestamp >= timestamp '2015-02-01 00:00:00' 
   group by 
      user_id 
     ,extract (year from timestamp::date)
     ,extract (month from timestamp::date)
 ) as dt
group by user_id
having count(*) = 36 -- one row per month
或者使用红移函数将时间戳截断为月1日,然后计数(不同):


按年份、月份分组的登录次数计数计数>0请发布数据样本。要回答这样的问题而不查看查询的运行情况要困难得多。我需要这些ID的列表,而不是count@PatJones它实际上是两列。用户id和时间戳。我不认为发布样本数据会有什么帮助,完全偏离了这一点。在提问时,发布样本数据是适当的程序,以便我们能够重现所讨论的情况。总是有多种方式让模糊性蔓延到问题中。按年份分组的登录次数计数,计数>0的月份请发布数据样本。要回答这样的问题而不查看查询的运行情况要困难得多。我需要这些ID的列表,而不是count@PatJones它实际上是两列。用户id和时间戳。我不认为发布样本数据会有什么帮助,完全偏离了这一点。在提问时,发布样本数据是适当的程序,以便我们能够重现所讨论的情况。歧义总是有多种方式潜入问题。我记得,Amazon Redshift在
count(distinct)
方面有问题,所以这绝对是最好的方式。我记得,Amazon Redshift在
count(distinct)
方面有问题,所以这绝对是最好的方式。