I have a difficult problem that makes me scratch my head a bit. I have some data that represent the delivery of "widgets" for a variable number of days, broken by half an hour.
For example (apologies for formatting - they did not quite understand):
Date Time NoOfUnits
01-Mar-2010 00:00 0
01-Mar-2010 00:30 0
01-Mar-2010 01:00 0
.... (following half hour intervals have NoOfUnits = 0)
01-Mar-2010 23:00 10
01-Mar-2010 23:30 10
02-Mar-2010 00:00 10
.... (following half hour intervals have NoOfUnits = 1)
02-Mar-2010 07:00 10
02-Mar-2010 07:30 0
.... (following half hour intervals have NoOfUnits = 0)
02-Mar-2010 23:30 0
I need to generate a query that allows me to group this data into all the individual blocks where I deliver the unit. In the above example, I need to determine only 1 block - 23:00 - 07:00 and the sum of units for this block (160). Therefore, the desired result will be StartTime, EndTime, TotalNoOfUnits.
However, the difficulty arises when we have different delivery options - maybe we have a day when we deliver units within 24 hours.
StartTime, EndTime TotalNoOfUnits, NoOfUnits < > 0.
. , , .
: , 00:00 23:30 , . , .
EDIT2: script, 2 . . , , , 13:00, 00:00, 230. , SQL- , !
declare @DayCount int
declare @HalfHourCount int
declare @HH int
declare @CurrentDate datetime
declare @BaseDate datetime
declare @NoOfUnits int
set @HalfHourCount = 48
set @DayCount = 4
set @BaseDate = '1 Jan 1900'
create table
(
Date datetime
, Time datetime
, NoOfUnits int
)
while @DayCount > 0
begin
set @CurrentDate = dateadd(dd, @DayCount * -1, CONVERT(VARCHAR(10),GETDATE(),111))
set @HH = @HalfHourCount
while @HH > 0
begin
if @HH > 24
set @NoOfUnits = 10
else
begin
if @DayCount = 4 and @HH < 10
set @NoOfUnits = 10
else
set @NoOfUnits = 0
end
insert into
values (@CurrentDate, dateadd(mi, @HH / 2.0 * 60, @BaseDate), @NoOfUnits)
select @HH = @HH - 1
end
set @DayCount = @DayCount - 1
end
( 00:00 23:00 00:30 00:00):
StartTime TotalUnits EndDate
----------------------- ----------- -----------------------
1900-01-01 00:30:00.000 90 1900-01-01 04:30:00.000
1900-01-01 12:30:00.000 960 1900-01-02 00:00:00.000