Jennifer Lisser (je*************@dorner.com) writes:
John - I tried doing the temporary table but wasn't able to. I
neglected to mention my database is EMS/RMS and I don't have rights to
create a table, only update the one existing table. I can update and
select via standard SQL queries but am limited in other functions.
There will be thousands of part numbers in this table so I'm not sure
what my best approach would be (an array of used or available numbers
may cripple the page).
In MS SQL Server everyone has the rights to create temp tables.
Now you mention EMS/RMS that I don't know what it is, but reviewing your
attempt to a query, I see that inlucdes syntax ("maxrows 5") which is
not legal in SQL Server. And Microsoft SQL Server is what this group is
devoted to.
In any case, you might have misunderstood the purpose of the CREATE
TABLE statement in John's posting. It is customary when you ask a question
to provide CREATE TABLE statements for the tables involved, and INSERT
statements with sample data. This increases your chances to get a good
reply. Now, the great thing with John, is that he actually does the
work for you. So from John you get a tested script which proves that
you get the desired result. But you still need to transform into your
database.
Now, John solution used views, and if you don't have privileges to
create views, you can try this query, still using his table:
SELECT *
FROM (SELECT first_consecutive = MIN(part_number), last_consecutive,
length = last_consecutive - MIN(part_number) + 1
FROM (SELECT P1.part_number,
MIN(P2.part_number) AS last_consecutive
FROM PartNumbersAvailable AS P1
LEFT JOIN (SELECT P1.part_number
FROM PartNumbersAvailable AS P1
LEFT JOIN PartNumbersAvailable AS P2
ON P2.part_number = P1.part_number + 1
WHERE P2.part_number IS NULL) AS P2
ON P2.part_number >= P1.part_number
GROUP BY P1.part_number) AS P
GROUP BY last_consecutive) AS P
WHERE length >= 5
ORDER BY first_consecutive
Here I have only expanded John's views into derived tables.
Whether EMS/RMS supports derived tables I don't know, but at least
this is a feature that is in ANSI-SQL, and not proprietary to MS SQL Server.
--
Erland Sommarskog, SQL Server MVP,
so****@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp