October 30, 2007 at 11:40 am
SQL2000
A table contains ID, CODE, and PRIORITY(varchar). For each ID Exists codes prioritized 1..n. Example :(ID, Code, PRI)
1 , A1, 01
1 , A1, 02
1 , F4, 03
1, D2, 04
ect.
How can I identify Codes that are missing or have gaps in the priority sequence. Example
2 , A3, 01
2 , A3, 02
2, D2, 04
2, D1, 05
Thanks.
October 30, 2007 at 11:59 am
I don't really see the missing sequence in your example. The ID doesn''t look unique and the priority apprears to be a char field. Here is a thread on finding missing sequences which may help you.
http://www.sqlservercentral.com/Forums/Topic398193-169-1.aspx
October 30, 2007 at 12:19 pm
select t.id,t.code,t.pri
from table1 t left outer join
table1 u on t.id=u.id and t.pri=u.pri+1
where u.id is null and t.id>1
Should return all of the rows with a gap in priority BEFORE it.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 30, 2007 at 12:29 pm
October 30, 2007 at 12:37 pm
Isn't that what his example is showing? gaps in prio by id?
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 30, 2007 at 12:55 pm
October 30, 2007 at 2:12 pm
Rough and crude but seems to answer the question:
SELECT Count(Code)AS 'Count of Code entries',Code, Max(Pri)AS 'Max Pri', Min(pri) AS 'Min Pri',
Cast(Max(Pri) AS Int) - CAST(Min(Pri)AS Int)+ 1 AS 'Diff'
FROM Table1
GROUP BY Code
HAVING CAST(Max(Pri) AS Int) > COUNT(CODE)
October 31, 2007 at 5:19 am
Sorry for the confusion.
I'm looking for gaps in the sequence. The ID and Code are just record fields, and I need to know if someone skipped a sequence number. I know it's a weird case, I'm not sure how you repreresent something thats NOT there?
Randy
October 31, 2007 at 6:07 am
rmcknight (10/31/2007)
Sorry for the confusion.I'm looking for gaps in the sequence. The ID and Code are just record fields, and I need to know if someone skipped a sequence number.
Then you need to tell your database what's your sequence.
Create a table with all numbers in your sequence.
After that you'll need just simple LEFT JOIN.
Nothing weird.
_____________
Code for TallyGenerator
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply