sqlserver 多个时间合并成时间段

CodeofWorker CodeofWorker 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
CREATE TABLE #tmptable(
rq datetime NOT NULL PRIMARY KEY CLUSTERED,
sn int IDENTITY(1,1) NOT NULL,
userId int,
)
GO
INSERT #tmptable VALUES('20170703', 1)
INSERT #tmptable VALUES('20170704', 1)
INSERT #tmptable VALUES('20170705', 1)
INSERT #tmptable VALUES('20170706', 1)
INSERT #tmptable VALUES('20170707', 1)
INSERT #tmptable VALUES('20170708', 1)
INSERT #tmptable VALUES('20170710', 1)
INSERT #tmptable VALUES('20170711', 1)
INSERT #tmptable VALUES('20170712', 1)
INSERT #tmptable VALUES('20170713', 1)
INSERT #tmptable VALUES('20170714', 1)
INSERT #tmptable VALUES('20170715', 1)
INSERT #tmptable VALUES('20170717', 1)
INSERT #tmptable VALUES('20170714', 2)
INSERT #tmptable VALUES('20170715', 2)
INSERT #tmptable VALUES('20170717', 2)
GO

能不能不用循环之类的将它们合并成一条记录:

开始日期 结束日期 USERID
=========== ================= =========
2017-07-03 2017-07-17 1
2017-07-14 2017-07-17 2
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
CodeofWorker
疑难问题 22,298 成员 121,727 内容