我知道一些基本的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
不幸的是,应用程序层需要基表采用show格式。这可以在查询中做吗?
谢谢
WITH q AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY crew, dayType ORDER BY [date]) AS rnd, ROW_NUMBER() OVER (PARTITION BY crew ORDER BY [date]) AS rn FROM mytable ) SELECT MIN([date]), MAX([date]), crew AS name, dayType FROM q GROUP BY crew, dayType, rnd - rn