Помогите пожалуйста составить оптимальный запрос типа over .. Partition (хотя другая модель тоже подойдёт) или что там недорогое по нагрузке на сервер будет в реальном времени.
> Необходимо для каждого студента найти общую продолжительность занятий начиная (т.е. после) каждого из 3-х последних экзаменов.
Насколько я понимаю это через ..RowNumber() over (Partition by StudentID Order By Date Desc) as rw .. Where rw=.. и потом Join-ы .. ну или что-то в этом роде.
Исходная таблица (StudentID,Type,Date,Duration) в самом низу
>Например на выходе нужно увидеть такое:
---------
StudentID__DateEx1___Lessons1__DateEx2__Lessons2__DateEx3___Lessons3
1284______4/2/2013____12_____1/22/2013___12____11/16/2012_____17
3079______2/15/2013___4______10/26/2012___8_______Null________Null
5045______2/18/2013__13______1/15/2013___17____12/09/2012_____17
---------
Выделить даты последних 3х экзаменов один за одним могу через RowNumber() over (partition.. , a вот как это в едином запросе соединить без временных таблиц и пользовательских функций, да с суммами за период - ну ни как не срастается
Заранее спасибо.
P.S. Система: SQLServer 2012
[more=скрипт создания исходной таблицы:]CREATE TABLE [dbo].[Book2](
[StudentID] [int] NOT NULL,
[Type] [varchar](8) NOT NULL,
[Date] [date] NOT NULL,
[Duration] [smallint] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x40360B00 AS Date), 4)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Exam', CAST(0x7D360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Exam', CAST(0x2B360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Lesson', CAST(0x31360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0x35360B00 AS Date), 3)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Lesson', CAST(0x6B350B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0x45360B00 AS Date), 5)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x47360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Lesson', CAST(0x4A360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x4D360B00 AS Date), 4)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Exam', CAST(0x51360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Lesson', CAST(0x6C350B00 AS Date), 3)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0xE6350B00 AS Date), 5)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Exam', CAST(0x42360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Exam', CAST(0x66360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0x6A360B00 AS Date), 3)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x43360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x84360B00 AS Date), 5)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Exam', CAST(0xA2360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0xA7360B00 AS Date), 4)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Exam', CAST(0xA9360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Lesson', CAST(0xB0360B00 AS Date), 4)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0xD0360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0x0D370B00 AS Date), 3)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Exam', CAST(0xC1360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0xC3360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Exam', CAST(0xC4360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0xCA360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (3079, N'Lesson', CAST(0x4B370B00 AS Date), 4)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x89370B00 AS Date), 3)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0xE8360B00 AS Date), 2)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0xEA360B00 AS Date), 3)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Exam', CAST(0xB6360B00 AS Date), 1)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (1284, N'Lesson', CAST(0x0F370B00 AS Date), 4)
INSERT [dbo].[Book2] ([StudentID], [Type], [Date], [Duration]) VALUES (5045, N'Lesson', CAST(0x6B370B00 AS Date), 5)[/more]