473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SQL Query - Find block of sequential numbers

I have a database that is pre-populated with sequential part numbers.
As people reserve the parts I update a flag to show the # is no longer
available. Now they want the ability to take out a block of "x"
number of sequential part numbers - say for example 5.

If my database had the following numbers available:
101
104
105
110
111
112
113
114

It should return 110 thru 114 and then I would write an update query
to change the flags to 1 (checked out).

I have only been able to return the first "x" number of records - have
not been able to make sure they are stepped sequentially - with the
following:

SELECT ID_ITEM From PARTNO_CHKOUT_SPECIAL M Where (Select Count(*)
FROM PARTNO_CHKOUT_SPECIAL N
WHERE N.ID_ITEM <= M.ID_ITEM) >= 0 AND TYPE_REC=1 AND
FLAG_CHECKED_OUT=0 {maxrows 5}

The above would return 101, 104, 105, 110, 111

I tried using an (N.ID_ITEM+1)-M.ID_ITEM=0 to try stepping and get
errors, probably incorrect syntax. Can I do this in an SQL statement?
Jul 20 '05 #1
6 12679
"Jenn L" <jm******@dorner.com> wrote in message
news:61**************************@posting.google.c om...
I have a database that is pre-populated with sequential part numbers.
As people reserve the parts I update a flag to show the # is no longer
available. Now they want the ability to take out a block of "x"
number of sequential part numbers - say for example 5.

If my database had the following numbers available:
101
104
105
110
111
112
113
114

It should return 110 thru 114 and then I would write an update query
to change the flags to 1 (checked out).

I have only been able to return the first "x" number of records - have
not been able to make sure they are stepped sequentially - with the
following:

SELECT ID_ITEM From PARTNO_CHKOUT_SPECIAL M Where (Select Count(*)
FROM PARTNO_CHKOUT_SPECIAL N
WHERE N.ID_ITEM <= M.ID_ITEM) >= 0 AND TYPE_REC=1 AND
FLAG_CHECKED_OUT=0 {maxrows 5}

The above would return 101, 104, 105, 110, 111

I tried using an (N.ID_ITEM+1)-M.ID_ITEM=0 to try stepping and get
errors, probably incorrect syntax. Can I do this in an SQL statement?


CREATE TABLE PartNumbersAvailable
(
part_number INT NOT NULL PRIMARY KEY
)

INSERT INTO PartNumbersAvailable (part_number)
VALUES (101)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (104)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (105)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (110)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (111)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (112)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (113)
INSERT INTO PartNumbersAvailable (part_number)
VALUES (114)

-- All part numbers P where there doesn't exist a part number P+1
-- Broken out of the next query for clarity
CREATE VIEW NoConsecutivePartNumbers (part_number)
AS
SELECT P1.part_number
FROM PartNumbersAvailable AS P1
LEFT OUTER JOIN
PartNumbersAvailable AS P2
ON P2.part_number = P1.part_number + 1
WHERE P2.part_number IS NULL

-- All part number sequences
CREATE VIEW PartNumberSequences
(first_consecutive, last_consecutive, sequence_length)
AS
SELECT MIN(part_number),
last_consecutive,
last_consecutive - MIN(part_number) + 1
FROM (SELECT P1.part_number,
MIN(P2.part_number) AS last_consecutive
FROM PartNumbersAvailable AS P1
LEFT OUTER JOIN
NoConsecutivePartNumbers AS P2
ON P2.part_number >= P1.part_number
GROUP BY P1.part_number) AS P
GROUP BY last_consecutive

SELECT *
FROM PartNumberSequences
ORDER BY first_consecutive

first_consecutive last_consecutive sequence_length
101 101 1
104 105 2
110 114 5

-- longest sequences
SELECT *
FROM PartNumberSequences
WHERE sequence_length = (SELECT MAX(sequence_length)
FROM PartNumberSequences)

first_consecutive last_consecutive sequence_length
110 114 5

--
JAG
Jul 20 '05 #2
Hi Jenn,

I'm not clear what you want. In general, if I have a complicated
UPDATE, I like to use UPDATE FROM. I first select the items that I
want and save it in a temp table. Then JOIN using the UPDATE FROM.

Something like:
Select top 5 ID_ITEM
Into #T
From PARTNO_CHKOUT_SPECIAL
Order by ID_ITEM Descending

Update PARTNO_CHKOUT_SPECIAL
Set Flag=1
From PARTNO_CHKOUT_SPECIAL a
Join #T b
On a.ID_ITEM=b.ID_ITEM

Which can also be rewritten as:
Update PARTNO_CHKOUT_SPECIAL
Set Flag=1
From
(Select top 5 ID_ITEM From PARTNO_CHKOUT_SPECIAL Order by ID_ITEM
Descending) b
On PARTNO_CHKOUT_SPECIAL.ID_ITEM=b.ID_ITEM

jm******@dorner.com (Jenn L) wrote in message news:<61**************************@posting.google. com>...
I have a database that is pre-populated with sequential part numbers.
As people reserve the parts I update a flag to show the # is no longer
available. Now they want the ability to take out a block of "x"
number of sequential part numbers - say for example 5.

If my database had the following numbers available:
101
104
105
110
111
112
113
114

It should return 110 thru 114 and then I would write an update query
to change the flags to 1 (checked out).

I have only been able to return the first "x" number of records - have
not been able to make sure they are stepped sequentially - with the
following:

SELECT ID_ITEM From PARTNO_CHKOUT_SPECIAL M Where (Select Count(*)
FROM PARTNO_CHKOUT_SPECIAL N
WHERE N.ID_ITEM <= M.ID_ITEM) >= 0 AND TYPE_REC=1 AND
FLAG_CHECKED_OUT=0 {maxrows 5}

The above would return 101, 104, 105, 110, 111

I tried using an (N.ID_ITEM+1)-M.ID_ITEM=0 to try stepping and get
errors, probably incorrect syntax. Can I do this in an SQL statement?

Jul 20 '05 #3
Thank you both for your quick responses!

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).

Louis - The update is easy especially if they want to take one number
for one special job. The problem is engineers want numbers in sequence.
For example, they need 25 sequential part numbers in the 75xxxx series,
somehow I would have to query the table to find 750200 thru 750224 were
available before doing the update. The logic of stepping through to see
if I have the sequence of numbers available is the tough part.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
"Jennifer Lisser" <je*************@dorner.com> wrote in message
news:40*********************@news.frii.net...
Thank you both for your quick responses!

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).

Louis - The update is easy especially if they want to take one number
for one special job. The problem is engineers want numbers in sequence.
For example, they need 25 sequential part numbers in the 75xxxx series,
somehow I would have to query the table to find 750200 thru 750224 were
available before doing the update. The logic of stepping through to see
if I have the sequence of numbers available is the tough part.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


The solution I offered does not use a temp table. The one table I created
was meant to simply be a stand-in for the table in your database that
has available part numbers. Simply substitute your table name in the
code provided and all should be fine.

--
JAG
Jul 20 '05 #5
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
Jul 20 '05 #6
"Erland Sommarskog" <so****@algonet.se> wrote in message news:Xn**********************@127.0.0.1...
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.


And this time around, you've done the work for me. Many thanks!
Glad I never miss reading an Erland post.

Warm regards,
John
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

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
3
by: James | last post by:
Hi all, I am trying to write a query to do the following in SQL server but am struggling: I have a table with a int number field in it and I want to find out the lowest unused number in the...
4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
7
by: Melissa | last post by:
I'm trying to create a function that I can put in a query field that will consecutively number the records returned by the query starting at 1 and will start at 1 each time the query is run. So far...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
4
by: Mahesh BS | last post by:
Hello, I need to write a query to find out a set of missing number in a given sequence. Eg : a Column in some table has the following data
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down the side and the RANKING of the YTD dept sales...
1
by: Vinod Sadanandan | last post by:
A Roadmap To Query Tuning ============================ For each SQL statement, there are different approaches that could be used to retrieve the required data. Optimization is the process of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.