给出下表
ID Teacher lessondate period Group Course Room Lnr Count
167 teacher 1 2013-12-02 08:30:00.000 1 group 1 NE room 1 223 1
168 teacher 1 2013-12-02 09:15:00.000 2 group 1 NE room 1 224 1
169 teacher 1 2013-12-02 10:00:00.000 3 group 1 ENG room 1 226 1
170 teacher 1 2013-12-02 11:00:00.000 4 group 1 VAKT room 1 228 1
171 teacher 1 2013-12-02 11:45:00.000 5 group 1 DGV room 1 227 1
172 teacher 1 2013-12-02 13:00:00.000 6 group 1 VAKT room 1 228 1
183 teacher 1 2013-12-05 08:30:00.000 1 group 1 VAKT room 2 228 1
184 teacher 1 2013-12-05 09:15:00.000 2 group 1 NE room 2 223 2
185 teacher 1 2013-12-05 10:00:00.000 3 group 1 NE room 2 223 2
186 teacher 1 2013-12-05 11:00:00.000 4 group 1 LB room 1 222 2
187 teacher 1 2013-12-05 11:45:00.000 5 group 1 LB room 1 222 2
188 teacher 1 2013-12-05 13:00:00.000 6 group 1 REK room 1 225 1
189 teacher 1 2013-12-06 14:45:00.000 8 group 2 AAA 4 3
190 teacher 1 2013-12-06 15:30:00.000 9 group 2 AAA 4 3
191 teacher 1 2013-12-06 16:15:00.000 10 group 2 AAA 4 3
我想在SQL中使用以下条件生成count列:
计数是>如果期间在同一天且连续(期间)且课程编号(lnr)相等,则为1。
数据说明:
我猜它有分区,等等,但我的sql知识缺乏那部分:(
* 编辑: *我到目前为止的查询:
select distinct a.*, blockHours
from timetablerecords a
join (
select distinct lessonnumber, convert(date,c.lessonDate) ld, count(distinct period) blockHours
from timetablerecords c
where teacher='teacher 1'
group by convert(date,c.lessonDate), c.lessonnumber, c.course
) b
on a.lessonNumber=b.lessonnumber and convert(date,a.lessonDate)= b.ld
where teacher='teacher 1'
order by lessondate,period
这不考虑不连续(或不连续?)期间。使用上面的数据,对于id 170和172
,它返回count = 2查询在MS SQL Azure数据库上运行 输出应包含表中提到的所有列。只有上面没有显示的列,但结果也应该是“持续时间” 可以使用以下脚本创建表:
CREATE TABLE [dbo].[timetablerecords](
[Id] [int] IDENTITY(1,1) NOT NULL,
[teacher] [nvarchar](255) NOT NULL,
[lessonDate] [datetime] NOT NULL,
[period] [int] NOT NULL,
[group] [nvarchar](255) NOT NULL,
[course] [nvarchar](255) NOT NULL,
[room] [nvarchar](255) NOT NULL,
[lessonNumber] [nvarchar](255) NOT NULL,
[lessonDuration] [int] NOT NULL,
[locationCode] [nvarchar](255) NOT NULL)