SQL判断是否已经过去了连续几个月
我有 3 列,第 1 列表示代理 ID,第 1 列表示他们所处的级别(初级、中级、高级),第 1 列表示他们达到该级别的月末日期。
如果示例数据如下所示,
360123, Beginner, 1/22/2011
360123, Null, 2/22/2011
360123, Beginner, 3/22/2011
360123, Intermediate, 4/22/2011
360123, Beginner, 5/22/2011
360123, Beginner, 6/22/2011
我如何设计一个查询来告诉我从 3/22 到 6/22 连续 4 个月哪些代理实现了所有初学者目标?
哇,谢谢大家的帮助!
6 个月期间中的 4 个月(其中第一个月是您实现其中一个目标的任何一个月)怎么样?
I have 3 columns 1 indicating agent id, one indicating which level they are (Beginner, Intermediate, Advanced) and on indicate the month end date they reached that level.
If the sample data looks like this
360123, Beginner, 1/22/2011
360123, Null, 2/22/2011
360123, Beginner, 3/22/2011
360123, Intermediate, 4/22/2011
360123, Beginner, 5/22/2011
360123, Beginner, 6/22/2011
How do I design a query which will tell me which agents made all the beginner goals for 4 consecutive months from 3/22 to 6/22?
Wow, thank you all for the help!
How about for 4 months out of a 6 month period where the first month is any month you hit one of the goals?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
示例数据和示例如下。基本上,按 ID 分组并搜索不同月份的计数。
Sample data and example below. Basically, group by the ID and search for a count of distinct months.
主要技巧是创建“人工”Id 以将其用作递归 CTE 中的锚点:
DECLARE @T TABLE (
身份证号 INT ,
等级 VARCHAR (100),
日期字段 SMALLDATETIME);
Main trick is to create "artificial" Id to use it as anchor in recursive CTE:
DECLARE @T TABLE (
Id INT ,
Lvl VARCHAR (100),
Datefield SMALLDATETIME);