Tom Moreau
3/23/2007 2:31:00 PM
Why not populate a table with all of the seconds and then do the following:
select
s.Counter
, sum (case when category = 1 then 1 else 0 end) as Ready
, sum (case when category = 2 then 1 else 0 end) as Down
, sum (case when category = 3 then 1 else 0 end) as Delay
, sum (case when category = 4 then 1 else 0 end) as Etc
from
Seconds s
left join
hist_statusevents e on e.starttime <= s.Counter and s.Counter< e.endtime
group by
s.Counter
--
Tom
----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
.
"Aussie Matt" <AussieMatt@discussions.microsoft.com> wrote in message
news:FE685DC0-D881-4684-BE9C-0CD9E2C06962@microsoft.com...
Hello, I am hoping you can help me out, I am teaching myself SQL and I have
a
problem that I don;t understand the solution for, Attached is the stored
procedure.
The actual stored procedure run's and actually gives me the results I am
after, but it seems to run a little slow, I am wondering if someone can tell
me if there is a smarter way to right it that might speed up the execution
time,
To give you some background on the hist_statusevents table it looks like
this
shiftindex eqmt category starttime endtime .......
27170 RD50 1 0 10000
27170 RD50 2 10000 15000
27170 RD50 4 15000 28000
27170 RD50 1 28000 43200
27170 RD62 4 0 19500
27170 RD62 7 19500 22400
27170 RD62 1 22400 29000
27170 RD62 4 29000 43200
there are approx 50 different RD entires (these represent equipment) the
category represents the status of the equipment, Ready, Down, Delay etc...
The starttime and endtime is the timestamp of an event change, this time
representes in seconds from 6:30am in the morning the time the event occurs
into the day, (43200 is the number of seconds in a 12 hour period)
The intention of this Stored Procedure is to give a second by second look at
the status of the fleet showing the number of units in the Ready,Down, Delay
state to help give a picture of our performance.
The output of the Stored Procedure should look somthing like
ShiftIndex Counter ReadyTrucks AvailTrucks DownTrucks
27170 0 10 5 7
27170 1 10 5 7
27170 2 10 5 7
27170 3 10 5 7
.
.
27170 6000 9 5 8
27170 6000 10 5 8
27170 6000 10 5 8
.
.
.
.
27170 43200 5 10 8
CREATE PROCEDURE USP_FMSRX155 (@X int, @Y int) AS
declare @counter int
declare @ReadyTrucks int
declare @AvailTrucks int
declare @DownTrucks int
declare @value int
set @counter = 0
set @ReadyTrucks = 0
set @AvailTrucks = 0
set @DownTrucks = 0
delete from TIMECAT
while (@counter <= 43200)
begin
set @ReadyTrucks = isnull((select count(starttime) from hist_statusevents
where shiftindex between @X and @Y and category = 1 and @Counter between
starttime and endtime and eqmt like 'RD%'),0)
set @AvailTrucks = isnull((select count(starttime) from hist_statusevents
where shiftindex between @X and @Y and category in(3,2,7) and @Counter
between starttime and endtime and eqmt like 'RD%'),0)
set @DownTrucks = isnull((select count(starttime) from hist_statusevents
where shiftindex between @X and @Y and category in(4,5) and @Counter
between
starttime and endtime and eqmt like 'RD%'),0)
insert into TIMECAT values (@X,
@counter,@ReadyTrucks,@AvailTrucks,@DownTrucks)
set @counter = @counter +1
end
select * from TIMECAT
GO
Appreciate any help
Cheers
Matt