SQL分组时日期之间的差异
我有一个包含 user_id 和 date_of_purchase 的购买表。
我需要能够选择在 12 个月内相互购买过 2 次的所有用户。日期可以是任何时间点,只要它们之间的间隔小于 12 个月即可。
例如,
user_id date_of_purchase
123 01/Jan/2010
124 01/Aug/2010
123 01/Feb/2010
124 05/Aug/2008
在这个例子中我想要 user_id 123
I have a table of purchases containing a user_id and a date_of_purchase.
I need to be able to select all the users who have made 2 purchases within 12 months of each other. The dates can be any point in time as long as they are less than 12 months apart.
e.g.
user_id date_of_purchase
123 01/Jan/2010
124 01/Aug/2010
123 01/Feb/2010
124 05/Aug/2008
In this example i want user_id 123
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:这不处理闰年。
Note: this does not handle leap years.
在 Oracle 中,有一个 DATE_DIFF 函数 - 我认为 MS 等效函数没有下划线 (DATEDIFF)。
In Oracle, there's a DATE_DIFF function - I think the MS equivalent has no underscore (DATEDIFF).