我很难想出一个合适的头衔。这就是问题所在。我试图确定每月新用户,以及他们何时进入该工具并制定计划。
这是我到目前为止所拥有的
select
distinct userID,
MIN(planID),
MIN(PlanCreated)
from dataBaseName.tableName
Group by userID
这为我提供了我需要的地面信息。我得到了用户和他们创建的第一个计划以及何时。
但是我需要进一步完善这一点。因为有些用户会进入该工具并且只创建计划,所以将其留在' Draft'地位,永不回归。我需要过滤掉超过3个月的草稿计划。
有一个' PlanStatus'基于' Draft'过滤的列状态很简单
where PlanStatus
is not in 'Draft'
但同样,我只想从结果中删除超过3个月的计划。
离。日期格式 - 2017-01-01 00:00:00
我还不确定是否应首先执行此过滤器,以便在以后创建计划并将其移出草稿状态时,不会完全删除ID。
示例数据:
planID | userID | PlanCreated | PlanStatus | OtherColumns
1111 | 2 | 2016-01-17 00:00:00 | Completed | null
1112 | 1 | 2016-06-31 00:00:00 | Draft | null
1113 | 2 | 2017-01-24 00:00:00 | Completed | null
1114 | 3 | 2017-02-04 00:00:00 | Draft | null
1115 | 1 | 2017-03-12 00:00:00 | Draft | null
预期结果:
planID | userID | PlanCreated | PlanStatus | OtherColumns
1111 | 2 | 2016-01-17 00:00:00 | Completed | null
1114 | 3 | 2017-02-04 00:00:00 | Draft | null
1115 | 1 | 2017-03-12 00:00:00 | Draft | null
请帮忙
答案 0 :(得分:0)
首先,您在哪里学习使用select distinct
聚合函数。正确的语法是:
select userID, min(planID), min(PlanCreated)
from dataBaseName.tableName
Group by userID;
不需要distinct
。然后你想要一个where
。这是一种方式:
select userID, min(planID), min(PlanCreated)
from dataBaseName.tableName
where not (status = 'draft' and plandate < '2017-01-01')
Group by userID;
请注意,日期函数和常量可能因数据库而异。
答案 1 :(得分:0)
试试这个:
SELECT userID, MIN(planID) , MIN(PlanCreated) FROM dataBaseName.tableName
WHERE NOT (status = 'draft' AND DATEDIFF(DAY, plancreated, getDate()) > 90)
GROUP BY userID