如何对连续范围进行分组
我知道一些基本的 SQL,但这个超出了我的能力范围。我看了高低,但没有骰子。我需要查看以下数据,我可以在应用程序层代码中执行此操作。但不幸的是,对于这一特定的情况,代码必须放在数据层中。
我正在使用 T-SQL。
表
Date Crew DayType
01-02-11 John Doe SEA
02-02-11 John Doe SEA
03-02-11 John Doe SEA
04-02-11 John Doe HOME
05-02-11 John Doe HOME
06-02-11 John Doe SEA
我需要这样的视图
DateFrom DateTo Name DayType
01-02-11 03-02-11 John Doe SEA
04-02-11 05-02-11 John Doe HOME
06-02-11 06-02-11 John Doe SEA
不幸的是,应用程序层需要基表才能采用所示的格式。这可以在查询中执行吗?
I know some basic SQL, but this one is beyond me. I have looked high and low but no dice. I need a view of the following data, I can do this in the application layer code. But unfortunately for this particular one, the code must be put in the data layer.
I am using T-SQL.
Table
Date Crew DayType
01-02-11 John Doe SEA
02-02-11 John Doe SEA
03-02-11 John Doe SEA
04-02-11 John Doe HOME
05-02-11 John Doe HOME
06-02-11 John Doe SEA
I need a view like this
DateFrom DateTo Name DayType
01-02-11 03-02-11 John Doe SEA
04-02-11 05-02-11 John Doe HOME
06-02-11 06-02-11 John Doe SEA
Unfortunately, the base table is required for the application layer to be in the format shown. Is this possible to do in a query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能会对本文感兴趣:
This article may be of interest to you:
基本上与 Quassnoi 的解决方案相同,但少使用一个
ROW_NUMBER
会产生更好的执行计划。Basically, same as Quassnoi's solution, but using one
ROW_NUMBER
fewer yields a better execution plan.像这样的东西:
Something Like:
试试这个。
try this.