March 14, 2007 at 11:05 am
I have a part that goes through a sequence of status's. I am looking to simplifiy my stored procedure.
the SQL below works, Is there another method for retrieving startWeight instead of the case statement? How efficient is a case statement in the sql below?
Select job.name job, sequence.name sequence
, count(*) TotalCount , sum(part.weight) TotalWeight
, count(start.id) StartCount
, sum(case when start.ID is not null then part.weight else 0 end) startWeight
, count(inspect.id) InspectCount
, count(ship.id) ShipCount
From job
INNER JOIN part ON..
INNER JOIN sequence ON..
LEFT JOIN (select distinct partID from status where status = start) start ON..
LEFT JOIN (select distinct partID from status where status = inspect) inspect ON..
LEFT JOIN (select distinct partID from status where status = ship) ship ON..
WHERE job = 'j1'
Group By ...
thanks
March 14, 2007 at 4:09 pm
Hard to tell with the criteria for the "ON" clauses missing...
--Jeff Moden
Change is inevitable... Change for the better is not.
March 14, 2007 at 5:43 pm
The actual sproc was quite a bit more involved but I used the same type of logic below, specifically the case logic. I did not notice a performance hit.
I added the ... missing details below.
Select job.name job, sequence.name sequence
, count(*) TotalCount , sum(part.weight) TotalWeight
, count(start.id) StartCount
, sum(case when start.ID is not null then part.weight else 0 end) startWeight
, count(inspect.id) InspectCount
, count(ship.id) ShipCount
From job
INNER JOIN part ON part.jobid = job.id
INNER JOIN sequence ON part.sequenceID = sequence.id
LEFT JOIN (select distinct partID from status where status = start) start ON part.id = start.partid
LEFT JOIN (select distinct partID from status where status = inspect) inspect ON part.id = inspect.partid
LEFT JOIN (select distinct partID from status where status = ship) ship ON part.id = ship.partid
WHERE job = 'j1'
Group By job.name job, sequence.name sequence
thanks
March 15, 2007 at 7:19 am
I'd likely make it use a few more CASE statements to keep from having to dip the Status table 3 times in the FROM clause... crosstab code is VERY fast if you let it be...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply