Second try
I was simplifying my question and example a little to much I think. Now maybe it becomes to complex.
if a given time Period is larger than available dates, just show available dates.
"Given a time period, sum the amount for each item by week number for all week numbers and items within the time period. "
The desired query let's me state a time period, and shows me a result as below within that stated period.
Aproximating the result to the next och previous week which ever is simplest, if the stated time period is not starting or ending at the same day as a week.
A Week starts on a Monday and ends on a Sunday.
declare
@YourTable table (MachineID int,MachineName varchar(10))
insertinto @YourTablevalues
(1, 'Machine A'),
(2, 'Machine B'),
(3, 'Machine C'),
declare
@YourTable table (RowID int,
MachineID int, OrderNr varchar(10),
StartDate datetime, StopDate datetime, Utilization int) insertinto @YourTablevalues
(1, 1, '00011','2012-01-02 00:01:00','2012-01-13 00:01:00', 33),
(2, 1, '00012','2012-01-09 00:01:00','2012-01-13 00:01:00', 33),
(3, 1,'00013','2012-01-16 00:01:00','2012-01-20 00:01:00', 33),
(4, 2,'00011','2012-01-02 00:01:00','2012-01-13 00:01:00', 20),
(5, 2, '00012','2012-01-09 00:01:00','2012-01-13 00:01:00', 15),
(6, 2,'00013','2012-01-09 00:01:00','2012-01-20 00:01:00', 65),
(7, 3,'00024','2012-01-02 00:01:00','2012-01-13 00:01:00', 33),
(8, 3, '00025','2012-01-05 00:01:00','2012-01-13 00:01:00', 66),
(9, 3,'00026','2012-01-09 00:01:00','2012-01-20 00:01:00',
1),
Result
MachineName WeekNr Utilization
Machine A 1
33
Machine A 2
66
Machine A 3
33
Machine B 1
20
Machine B 2
100
Machine B 3
65
Machine C 1 99
Machine C 2
100
Machine C 3
1