栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何获取不在30天内的下一个最小日期并将其用作SQL中的参考点?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何获取不在30天内的下一个最小日期并将其用作SQL中的参考点?

就像您的示例一样,#test是您的数据表:

;with cte1as(    select         ID, Date,         row_number()over(partition by ID order by Date) groupID    from #test),cte2as(    select ID, Date, Date as DateTmp, groupID, 1 as getRow from cte1 where groupID=1    union all    select         c1.ID,         c1.Date,         case when datediff(Day, c2.DateTmp, c1.Date) > 30 then c1.Date else c2.DateTmp end as DateTmp,        c1.groupID,         case when datediff(Day, c2.DateTmp, c1.Date) > 30 then 1 else 0 end as getRow    from cte1 c1    inner join cte2 c2 on c2.groupID+1=c1.groupID and c2.ID=c1.ID)select ID, Date from cte2 where getRow=1 order by ID, Date


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号