473,406 Members | 2,312 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,406 software developers and data experts.

Querying information between two values

Alright, I am going to to my best to explain this.

I have an item that has a range of consecutive numbers associated with it. So Item1 has the vaules of #start = 1111 and #end = 1113. I then have a list of about 2 thousand numbers that I need to associate to items, and the only way to do this is going to be to find them between the #start and #end of an item. So for example if I have this:

Item2 has #start = 1114 and #end = 1116 I would like this item to show as a result when I search for #1115.

I have no idea how to go about this since I have so many numbers to search for. It would be simple if it was only one number to find, I was just use the between function, but with this many I am facing quite the task. Also, when I pull the information, I need the results to be two columns. One column have the item, and the other column have the number that I searched for so I can tie the two together. I am not sure if this is even possible.... Anyone have any suggestions?
Dec 9 '06 #1
6 2172
michaelb
534 Expert 512MB
You was not clear on how your data is organized.
I can imagine table with two columns, where first one is the item, and second is a long string containing integer values separated by space.
Another option is to have these integer values in a different table associated with item by foreign key.

In the first case one way of getting what you need (not necessarily the best way) is to use the like operator:
Expand|Select|Wrap|Line Numbers
  1. select item, 115 from myTable where (' ' ||  item_numbers  || ' ') like '% 115 %';
115 here is an arbitrary number, probably would be replaced by a variable in your code.

Note the trick with pasting spaces around the column and around the value.
It is used to capture the first and the last numbers in the query (if this is the requirement)
These numbers have space on one side only, and otherwise will be missed.
Dec 9 '06 #2
Expand|Select|Wrap|Line Numbers
  1. Itemname      NumberStart       NumberEnd
  2. Item1         1115              1118
  3. Item2         1120              1125
  4. Item3         1126              1130
  5. Item4         1135              1140
  6. Item5         1142              1150
So this is essentially how the data is organized. Granted there is other information in the table that I am querying, but this is all that is relevant.

I have a list of numbers, and I need to find which item they are associated to. If I were to do this one by one, in the where section of the query I would use

Expand|Select|Wrap|Line Numbers
  1. where '1117' is between NumberStart and NumberEnd
This would return Item1 as the result. So I guess my question has two parts. First part:
Is it possible to do something like this with a list of about 2000 numbers? Also, the amount of ItemNames involved is in the 800,000 range. I am looking for a way to do this in one query instead of having to go through the list of 2000 numbers 1 by 1.

Second part:
In the results I would like to columns. First column being the ItemName, and the second column being the number that I searched for tied to the ItemName in the first column.

I hope this is a more accurate way of explaining things.
Dec 9 '06 #3
Apparently my edit window has expired, but that needs to read "two columns" in the results. My apologies.
Dec 9 '06 #4
michaelb
534 Expert 512MB
I'll try to answer, although I'm not an Oracle expert, and I hope we have some in the community, so they'll correct me where I'm wrong...

>> First part:
>> Is it possible to do something like this with a list of about 2000 numbers?
If these numbers are dynamically generated and passed with the query you'll have to consider the limit on the query string. In the older days some APIs (such as ODBC and others) had a very small query max-size, about 1024 bytes.
Oracle itself, unless I'm mistaken puts this limit at 16MB

>> Second part:
>> In the results I would like to columns. First column being the ItemName,
>> and the second column being the number that I searched for tied to the
>> ItemName in the first column.

It is possible, at least with design where numbers are stored in a separate table - INUMBERS

Expand|Select|Wrap|Line Numbers
  1. select N.inumber, I.itemname from inumbers N, items I where N.inumber between I.numberstart and I.numberend;
  2. or
  3. select N.inumber, I.itemname from inumbers N join items I on (N.inumber between I.numberstar and I.numberend);
And you can also display all numbers used for query, those that don't have a match will have an empty second column:

Expand|Select|Wrap|Line Numbers
  1. select N.inumber, I.itemname from inumbers N left outer join items I on (N.inumber between I.numberstart and I.numberend);
  2.  
I tested this with a very small tables, but considering that your table ITEMS has 800000 records you will have to take a close look at performance.
I won't be surprised if on Windows it'll choke the system...
Dec 10 '06 #5
michaelb
534 Expert 512MB
Just realized that you're running on SQL server, not Oracle.
I'm very sorry about the confusion, chances are it all still applies, but you have to test the code yourself - I don't have SQL server available.
Dec 10 '06 #6
I am installing MSSQL on my local PC and I am going to load all of the numbers into a table within there so I can have that available. After this is done I will test this code and let you know how it goes.... crossing my fingers.


-onelung
Dec 18 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hakan Akkas | last post by:
Hello all, I need to build a search engine wherewith users can query huge Xml Documents (+/- 100 MB) in a user friendly way. The searcher shouldn't be aware of the underlying structure of the...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
8
by: charles.amith | last post by:
I have 2 tables: LOCATION and ELEVATION In location, I would like to find the record with the max value for field: DATE1 In elevation, I would like to find the record with the max value for...
1
by: VM | last post by:
I'm working on a win appication that is constantly querying a small-sized DB. Until now, I've been using Selects from within my app but, all of a sudden I remembered of an application I was...
2
by: Mrinal Kamboj | last post by:
Hi , Any idea regarding which class to use for querying the SCM and get various details , i actually want to build up a tool for accomplishing various taska of mine rather than doing them...
3
by: Mikolaj Machowski | last post by:
Hello, I am looking for tool to query Schema/RNG files to find: - all elements of current dialect - allowed children of given element - allowed attributes of given element - values of that...
2
by: trilokjain | last post by:
Hi, I want to know, how is automatic querying done. FOr example if I want to get information about flights from the website of an Airline, how can I do it automatically? Instead of filling the form...
2
by: runway27 | last post by:
i am building a registration page where a user register for a username. i am able to insert this into mysql. the situation is every time data is inserted into example table1 in mysql in database1 for...
1
by: SilRay | last post by:
I will admit up front that this is to help with an assignment for a class, however, I'm looking for information outside the scope of the assignment because I want to do something cooler. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.