- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Jeff最近问this question并得到了一些很好的答案。
Jeff 的问题围绕着查找连续 (n) 天登录系统的用户。使用数据库表结构如下:
Id UserId CreationDate------ ------ ------------750997 12 2009-07-07 18:42:20.723750998 15 2009-07-07 18:42:20.927751000 19 2009-07-07 18:42:22.283
阅读the original question首先是为了清楚,然后是……
我对确定用户的不同 (n) 天周期的问题很感兴趣。
是否可以设计一个快速的 SQL 查询来返回用户列表和他们拥有的不同 (n) 天时段的数量?
编辑:根据下面的评论 如果有人连续 2 天,然后是连续 4 天,然后是连续 8 天,然后是连续 8 天。这将是 3 个“不同的 4 天时间段”。 8 天的时间应计为两个背靠背的 4 天时间。
最佳答案
我的回答好像没有出现...
我再试一次...
Rob Farley 对原始问题的回答具有包含连续天数的便利好处。
with numberedrows as
(
select row_number() over (partition by UserID order by CreationDate) - cast(CreationDate-0.5 as int) as TheOffset, CreationDate, UserID
from tablename
)
select min(CreationDate), max(CreationDate), count(*) as NumConsecutiveDays, UserID
from numberedrows
group by UserID, TheOffset
使用整数除法,简单地除以连续的天数得到整个连续周期所涵盖的“不同的(n)天周期”的数量......
- 2/4 = 0
- 4/4 = 1
- 8/4 = 2
- 9/4 = 2
-等等等等
下面是我对 Rob 对您需求的回答...
(我真的很喜欢 Rob's answer,去阅读解释,它启发了思考!)
with
numberedrows (
UserID,
TheOffset
)
as
(
select
UserID,
row_number() over (partition by UserID order by CreationDate)
- DATEDIFF(DAY, 0, CreationDate) as TheOffset
from
tablename
),
ConsecutiveCounts(
UserID,
ConsecutiveDays
)
as
(
select
UserID,
count(*) as ConsecutiveDays
from
numberedrows
group by
UserID,
TheOffset
)
select
UserID,
SUM(ConsecutiveDays / @period_length) AS distinct_n_day_periods
from
ConsecutiveCounts
group by
UserID
唯一真正的区别是我采用 Rob 的结果,然后通过另一个 GROUP BY...运行它
关于SQL 来确定连续访问天数的不同时期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1176837/
我的目标是获取购买给定产品所需的平均天数。如果 Product_A 在给定时间段内购买了 3 次(“2012-12-01”、“2012-12-05”、“2012-12-10”),那么我们的平均订单间隔
我在计算利息天数时有一个有趣的错误。我每天检查并检查今天是哪一天(1 到 31)。现在我发现了一个问题:10月计数不正常。这意味着 27 号是 26 号,或者 29 号是 28 号。这是一个众所周知的
你好我一直在做一些程序,我的程序是获取一年中过去的天数。现在,当我尝试运行时,输出结果为“4438232”。 例如,如果用户输入 (mm-dd-yy) 3-18-2013,则该年经过的扩孔天数为 77
我看到此问题已针对 Java 得到解答, JavaScript , 和 PHP ,但不是 C#。那么,如何在 C# 中计算两个日期之间的天数? 最佳答案 假设 StartDate 和 EndDate
QQ每天抽超级会员成长值、天数 非必中 手Q打开链接进入活动,下拉页面随便分享一下领取抽奖机会,亲测3点成长值! 活动地址:https://sourl.cn/37CZad 手Q扫码:
是否有任何内置函数可以找出时间戳之间的天数、添加天数或查找时间戳之间的月数?目前,我将日期作为字符串存储在我的文档中。 例如,如果我这样做: return (DATE_TIMESTAMP("2014-
我无法弄清楚如何为以下场景编写公式。我需要根据另一列计算日期,但需要根据原始列的星期几添加天数。 If day of week equals 'Mon/Wed/Fri/Sat' then add 5
我有两个系列的 Pandas 约会时间。我为数据框中的每一行减去它们,并添加一列以获取两个日期时间之间的时间增量。随后我想使用该时间增量来扩展另一个功能。所以我想对那个时间增量进行一些划分。没有骰子。
这个问题已经有答案了: "cannot find symbol: method" but the method is declared (3 个回答) How to add one day to a
我的代码是这样的,但第三行出错 mm/dd/yyyy 我的数据库日期格式如下: SELECT evep year(date='MM/dd/yyyy'), month(da
我有两个 jQuery UI 日期选择器,当它们都选择了日期时,我希望这些日期之间的差异显示在单独的 input[type="text"] 一旦选择了第二个日期。 此外,理想情况下,我希望我的计数减去
我有两列:rental_date 和 actual_retdate。我需要找到 actual_retdate 和 rental_date 之间的天数。 actual_retdate 在某些情况下可以为
运行 MySql 和 Yii 我需要更改当前查找当月记录的关系查询以查找过去 45 天的所有记录。 这是我当前的查询: 'itemCount' => array(self::STAT, 'It
我想创建一个新列,它将显示两个日期之间的时间增量(以天为单位),如以下 pandas 数据框所示: >>> hg[['not inc','date']] not inc
我有: 年份数字(可以是任何年份) 月份数(从一月到十二月) 周数(第 1、2、3、4、最后) 工作日(周日、周一、周二、周三、周四、周五、周六) 我需要获取天数 [从 1 到 ~31] - "YYY
我需要为一些项目倒数 90 天、120 天和 160 天。我将如何编码?我一直在寻找 java 代码,但是在我为 android 开发时会出现错误。 我需要用今天的日期减去 x 天,然后将结果显示在屏
我的要求是计算给定的两个日期之间的天数,不包括星期六和星期日。 示例: Start date - 10/09/15 and End date 18/09/15 Result: 7 日期采用 DD/MM
我想比较我的 Android 应用程序的两个日期,但我遇到了一个非常奇怪的问题。 例如: 如果我将 回到过去 日期设置为 127 天前: this.dateEvent = System.current
如何返回所提供日期的 CumSum 天数? import pandas as pd df = pd.DataFrame({ 'date': ['2019-01-01','2019-01-03'
例如,我可以使用以下代码获取当前日期以显示在 DatePicker 对话框中。 final Calendar c = Calendar.getInstance(); mYear = c
我是一名优秀的程序员,十分优秀!