I'm having a difficult time figuring out how I can find out the first gap in a numeric col. I tried the faq and I just can't get it to work with my data. I have a table set up like this
schedID (bigint id), lineID (varchar), schedStatus (varchar), prodSeq (int)
I need to find the first gap in prodSeq (which is NOT the pkey), but I need to find it based on a certain status and line... IE:
schedID lineID schedStatus prodSeq
1000 W99 READY 1
1001 W99 COMPLETE 1
1002 W01 READY 1
1003 W99 READY 2
1004 W99 READY 5
1005 W99 COMPELTE 3
so I want to pull the first numeric "hole" for line W99 with a status of 'READY'
so the result should be 3
or if I want to pull for line W99 status COMPLETE
it should return 2
or line W01 status READY
should return 2
finally line W01 status COMPLETE
should return 1 (because there is nothing there)
I've been playing with a couple different queries and I just can't get them to work...but here they are...
SELECT a.ProductionSequence AS GapAfterSeq
FROM tblProductionSchedules a
WHERE NOT EXISTS( SELECT * FROM tblProductionSchedules b
WHERE b.ProductionSequence = a.ProductionSequence + 1 ) and
a.ProductionSequence < ( SELECT MAX( ProductionSequence ) FROM tblProductionSchedules )
AND a.CompletedStatus = 'READY' and a.ProductionLineID='W99'
****I know my real table has different names than my above examples...the examples are just smaller and easier to read***
thanks for any help
schedID (bigint id), lineID (varchar), schedStatus (varchar), prodSeq (int)
I need to find the first gap in prodSeq (which is NOT the pkey), but I need to find it based on a certain status and line... IE:
schedID lineID schedStatus prodSeq
1000 W99 READY 1
1001 W99 COMPLETE 1
1002 W01 READY 1
1003 W99 READY 2
1004 W99 READY 5
1005 W99 COMPELTE 3
so I want to pull the first numeric "hole" for line W99 with a status of 'READY'
so the result should be 3
or if I want to pull for line W99 status COMPLETE
it should return 2
or line W01 status READY
should return 2
finally line W01 status COMPLETE
should return 1 (because there is nothing there)
I've been playing with a couple different queries and I just can't get them to work...but here they are...
SELECT a.ProductionSequence AS GapAfterSeq
FROM tblProductionSchedules a
WHERE NOT EXISTS( SELECT * FROM tblProductionSchedules b
WHERE b.ProductionSequence = a.ProductionSequence + 1 ) and
a.ProductionSequence < ( SELECT MAX( ProductionSequence ) FROM tblProductionSchedules )
AND a.CompletedStatus = 'READY' and a.ProductionLineID='W99'
****I know my real table has different names than my above examples...the examples are just smaller and easier to read***
thanks for any help