如何检查值是否存在于上一个日期而不是当前日期
how to check if value exists in previous date and not in current date
下面是我的 table 并且有 ID、日期列
ID Date
1 2017-06-11
2 2017-06-11
3 2017-06-04
1 2017-06-04
4 2017-06-04
5 2017-06-04
6 2017-05-28
5 2017-06-04
2017-06-11 是本周开始日期,2017-06-04 是上周开始日期。
我想创建一个标志,其中 Id 存在于前一周而不是本周 1。
例如,ID 4 出现在前一周,而不是这周 week.similarly ID 6 出现在 2017-05-28 而不是 2017-06-04。
当我这周 query/analyse 我的数据时,我的数据应该看起来像
ID Date flag
1 2017-06-11 0
2 2017-06-11 0
3 2017-06-04 0
1 2017-06-04 0
4 2017-06-04 1
5 2017-06-04 0
同样地,当我分析我上周的数据时,
ID Date flag
3 2017-06-04 0
1 2017-06-04 0
4 2017-06-04 0
5 2017-06-04 0
6 2017-05-28 1
5 2017-06-04 0
我尝试使用 Left join 来检查 Id 在当前日期而不是在上一个日期是否存在,但是我得到的数据不准确,因为这必须是动态的。
当我每周查询时,我只需要比较本周和前一周,当我查询上周时,我只需要得到上周和 2017-05-28 的数据。
然后我将 tableau 中的过滤器提供给 select 日期并显示结果。
谁能帮我解决这个问题?
由于您希望查询结果取决于您要查看的周,我建议编写一个查询,以感兴趣的一周的开始和对应的前一周的开始作为参数。
如果我没理解错的话,那么您想要一个类似... "Take all records from the current and the previous week; for those records of the previous week, where no corresponding entry for the current week exists, write flag=1; else write flag=0" 的查询。然后下面的查询,直接 "translates" this 到 SQL 应该做的工作:
select
t1.*,
(case when t1.date = '2017-06-04'
and not exists
(select * from table t2 where t2.id = t1.id and t2.date = '2017-06-11')
then 1
else 0
end) as flag
from table t1
where t1.date = '2017-06-04' or t1.date = '2017-06-11'
计算您想要的一周的开始日期和return两周的值
DECLARE @date DATETIME = GETDATE()
DECLARE @firstDayWeek DATE =(SELECT convert(DATE,(DATEADD(dd,-DATEPART(WEEKDAY, @date)+1,@date))))
SELECT *
,CASE WHEN f.lastDate < @firstDayWeek THEN 1 ELSE 0 END flag
FROM tablee t1
OUTER APPLY( SELECT MAX(t2.date) lastDate
from tablee t2
WHERE t2.ID=t1.ID
AND t2.date < DATEADD(dd,7,@firstDayWeek )
) f
WHERE BETWEEN DATEADD(dd,-8,@firstDayWeek ) AND DATEADD(dd,6,@firstDayWeek )
下面是我的 table 并且有 ID、日期列
ID Date
1 2017-06-11
2 2017-06-11
3 2017-06-04
1 2017-06-04
4 2017-06-04
5 2017-06-04
6 2017-05-28
5 2017-06-04
2017-06-11 是本周开始日期,2017-06-04 是上周开始日期。 我想创建一个标志,其中 Id 存在于前一周而不是本周 1。
例如,ID 4 出现在前一周,而不是这周 week.similarly ID 6 出现在 2017-05-28 而不是 2017-06-04。 当我这周 query/analyse 我的数据时,我的数据应该看起来像
ID Date flag
1 2017-06-11 0
2 2017-06-11 0
3 2017-06-04 0
1 2017-06-04 0
4 2017-06-04 1
5 2017-06-04 0
同样地,当我分析我上周的数据时,
ID Date flag
3 2017-06-04 0
1 2017-06-04 0
4 2017-06-04 0
5 2017-06-04 0
6 2017-05-28 1
5 2017-06-04 0
我尝试使用 Left join 来检查 Id 在当前日期而不是在上一个日期是否存在,但是我得到的数据不准确,因为这必须是动态的。 当我每周查询时,我只需要比较本周和前一周,当我查询上周时,我只需要得到上周和 2017-05-28 的数据。
然后我将 tableau 中的过滤器提供给 select 日期并显示结果。
谁能帮我解决这个问题?
由于您希望查询结果取决于您要查看的周,我建议编写一个查询,以感兴趣的一周的开始和对应的前一周的开始作为参数。 如果我没理解错的话,那么您想要一个类似... "Take all records from the current and the previous week; for those records of the previous week, where no corresponding entry for the current week exists, write flag=1; else write flag=0" 的查询。然后下面的查询,直接 "translates" this 到 SQL 应该做的工作:
select
t1.*,
(case when t1.date = '2017-06-04'
and not exists
(select * from table t2 where t2.id = t1.id and t2.date = '2017-06-11')
then 1
else 0
end) as flag
from table t1
where t1.date = '2017-06-04' or t1.date = '2017-06-11'
计算您想要的一周的开始日期和return两周的值
DECLARE @date DATETIME = GETDATE()
DECLARE @firstDayWeek DATE =(SELECT convert(DATE,(DATEADD(dd,-DATEPART(WEEKDAY, @date)+1,@date))))
SELECT *
,CASE WHEN f.lastDate < @firstDayWeek THEN 1 ELSE 0 END flag
FROM tablee t1
OUTER APPLY( SELECT MAX(t2.date) lastDate
from tablee t2
WHERE t2.ID=t1.ID
AND t2.date < DATEADD(dd,7,@firstDayWeek )
) f
WHERE BETWEEN DATEADD(dd,-8,@firstDayWeek ) AND DATEADD(dd,6,@firstDayWeek )