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

Need Help on a query

db2 v 8.2 on AIX 5.3

I will try to explain as brief as I can what it is I need. I am
building a function that will be called multiple times where I will
need to return x amount of records each time for the same basic
query. I will be basically get passed in where I had left off the
last call but it is two fields that make up this unique index.

The main rule is that I cannot hold open a cursor on the record I last
read because it will be a long operation. That is the reason I will
be given the two fields to identify where I left off.

The trick is there is not a single column in this table that makes a
unique index so I cannot just order it like that. The two fields
together are the unique index.

In saying that, I am having trouble finding the right SQL to get my
cursor to the place I want to start my next set of queries...until all
the records that meet the criteria are read.

Here is an example:

COL1 and COL2 together are the unique index. I order by COL1, COL2
each time.

TABLE1

COL1 COL2 COL3
200 0 1 <---Start of the range <----*
200 2 1 <----*
200 6 1 <----*
300 0 1
300 2 1
300 6 1
300 15 1
300 18 1
400 0 1
400 6 1
400 7 1 <---End of the range
500 0 1
500 2 1
600 0 1
600 2 1
200 0 2
So, let us say I can only fetch 3 records at a time. The first time
the function is called I am told to get all records that have a COL3 =
1. I am also told the range that is needed from COL1.

So my query is something like "select COL1, COL2, COL3 from TABLE1
where COL3 = 1 and COL1 between 200 and 400 sort by COL1, COL2"

I would return the records indicated with a <---* above.

The next time the function is called parameters passed in tell me this
is not the first call by giving me back the last value of COL1 (which
is 200) and the last value of COL2(which is 6). So I know where I
left off but how do I get myself at that point again so that I can
continue and return the next 3 records?

I basically want to start the next fetch on the record right after
COL1 = 200 and COL2 = 6, and continue...until I hit the end of the
sort which would be where COL1 = 400.

This would be easy if I had one unique index that I can order but I am
not sure what to do with two where I cannot say WHERE a certain #.

Thanks for helping!

Mar 8 '07 #1
3 1913
On Mar 8, 9:20 am, "shorti" <lbrya...@juno.comwrote:
db2 v 8.2 on AIX 5.3

I will try to explain as brief as I can what it is I need. I am
building a function that will be called multiple times where I will
need to return x amount of records each time for the same basic
query. I will be basically get passed in where I had left off the
last call but it is two fields that make up this unique index.

The main rule is that I cannot hold open a cursor on the record I last
read because it will be a long operation. That is the reason I will
be given the two fields to identify where I left off.

The trick is there is not a single column in this table that makes a
unique index so I cannot just order it like that. The two fields
together are the unique index.

In saying that, I am having trouble finding the right SQL to get my
cursor to the place I want to start my next set of queries...until all
the records that meet the criteria are read.

Here is an example:

COL1 and COL2 together are the unique index. I order by COL1, COL2
each time.

TABLE1

COL1 COL2 COL3
200 0 1 <---Start of the range <----*
200 2 1 <----*
200 6 1 <----*
300 0 1
300 2 1
300 6 1
300 15 1
300 18 1
400 0 1
400 6 1
400 7 1 <---End of the range
500 0 1
500 2 1
600 0 1
600 2 1
200 0 2

So, let us say I can only fetch 3 records at a time. The first time
the function is called I am told to get all records that have a COL3 =
1. I am also told the range that is needed from COL1.

So my query is something like "select COL1, COL2, COL3 from TABLE1
where COL3 = 1 and COL1 between 200 and 400 sort by COL1, COL2"

I would return the records indicated with a <---* above.

The next time the function is called parameters passed in tell me this
is not the first call by giving me back the last value of COL1 (which
is 200) and the last value of COL2(which is 6). So I know where I
left off but how do I get myself at that point again so that I can
continue and return the next 3 records?

I basically want to start the next fetch on the record right after
COL1 = 200 and COL2 = 6, and continue...until I hit the end of the
sort which would be where COL1 = 400.

This would be easy if I had one unique index that I can order but I am
not sure what to do with two where I cannot say WHERE a certain #.

Thanks for helping!
WHERE COL3 = 1 and COL1 between 200 and 400
AND (COL1 last_col1
OR
COL1 = last_col1 AND COL2 last_col2
)
ORDER BY COL1, COL2
FETCH FIRST 3 ROWS ONLY

Mar 8 '07 #2
On Mar 8, 1:20 am, "shorti" <lbrya...@juno.comwrote:
db2 v 8.2 on AIX 5.3

I will try to explain as brief as I can what it is I need. I am
building a function that will be called multiple times where I will
need to return x amount of records each time for the same basic
query. I will be basically get passed in where I had left off the
last call but it is two fields that make up this unique index.

The main rule is that I cannot hold open a cursor on the record I last
read because it will be a long operation. That is the reason I will
be given the two fields to identify where I left off.

The trick is there is not a single column in this table that makes a
unique index so I cannot just order it like that. The two fields
together are the unique index.

In saying that, I am having trouble finding the right SQL to get my
cursor to the place I want to start my next set of queries...until all
the records that meet the criteria are read.

Here is an example:

COL1 and COL2 together are the unique index. I order by COL1, COL2
each time.

TABLE1

COL1 COL2 COL3
200 0 1 <---Start of the range <----*
200 2 1 <----*
200 6 1 <----*
300 0 1
300 2 1
300 6 1
300 15 1
300 18 1
400 0 1
400 6 1
400 7 1 <---End of the range
500 0 1
500 2 1
600 0 1
600 2 1
200 0 2

So, let us say I can only fetch 3 records at a time. The first time
the function is called I am told to get all records that have a COL3 =
1. I am also told the range that is needed from COL1.

So my query is something like "select COL1, COL2, COL3 from TABLE1
where COL3 = 1 and COL1 between 200 and 400 sort by COL1, COL2"

I would return the records indicated with a <---* above.

The next time the function is called parameters passed in tell me this
is not the first call by giving me back the last value of COL1 (which
is 200) and the last value of COL2(which is 6). So I know where I
left off but how do I get myself at that point again so that I can
continue and return the next 3 records?

I basically want to start the next fetch on the record right after
COL1 = 200 and COL2 = 6, and continue...until I hit the end of the
sort which would be where COL1 = 400.

This would be easy if I had one unique index that I can order but I am
not sure what to do with two where I cannot say WHERE a certain #.

Thanks for helping!
Not sure I fully understand but would something along the lines of:

select col1, col2, col3 from (
select COL1, COL2, COL3, row_number() over (
order by COL1, COL2 ) as rn
from TABLE1
where COL3 = 1 and COL1 between 200 and 400
) X where rn between 0 and 3

do? rn is the *cursor* position, so next time you run the query it
would be:

....
) X where rn between 4 and 7

etc
/Lennart

Mar 8 '07 #3
On Mar 7, 10:43 pm, "Tonkuma" <tonk...@jp.ibm.comwrote:
On Mar 8, 9:20 am, "shorti" <lbrya...@juno.comwrote:


db2 v 8.2 on AIX 5.3
I will try to explain as brief as I can what it is I need. I am
building a function that will be called multiple times where I will
need to return x amount of records each time for the same basic
query. I will be basically get passed in where I had left off the
last call but it is two fields that make up this unique index.
The main rule is that I cannot hold open a cursor on the record I last
read because it will be a long operation. That is the reason I will
be given the two fields to identify where I left off.
The trick is there is not a single column in this table that makes a
unique index so I cannot just order it like that. The two fields
together are the unique index.
In saying that, I am having trouble finding the right SQL to get my
cursor to the place I want to start my next set of queries...until all
the records that meet the criteria are read.
Here is an example:
COL1 and COL2 together are the unique index. I order by COL1, COL2
each time.
TABLE1
COL1 COL2 COL3
200 0 1 <---Start of the range <----*
200 2 1 <----*
200 6 1 <----*
300 0 1
300 2 1
300 6 1
300 15 1
300 18 1
400 0 1
400 6 1
400 7 1 <---End of the range
500 0 1
500 2 1
600 0 1
600 2 1
200 0 2
So, let us say I can only fetch 3 records at a time. The first time
the function is called I am told to get all records that have a COL3 =
1. I am also told the range that is needed from COL1.
So my query is something like "select COL1, COL2, COL3 from TABLE1
where COL3 = 1 and COL1 between 200 and 400 sort by COL1, COL2"
I would return the records indicated with a <---* above.
The next time the function is called parameters passed in tell me this
is not the first call by giving me back the last value of COL1 (which
is 200) and the last value of COL2(which is 6). So I know where I
left off but how do I get myself at that point again so that I can
continue and return the next 3 records?
I basically want to start the next fetch on the record right after
COL1 = 200 and COL2 = 6, and continue...until I hit the end of the
sort which would be where COL1 = 400.
This would be easy if I had one unique index that I can order but I am
not sure what to do with two where I cannot say WHERE a certain #.
Thanks for helping!

WHERE COL3 = 1 and COL1 between 200 and 400
AND (COL1 last_col1
OR
COL1 = last_col1 AND COL2 last_col2
)
ORDER BY COL1, COL2
FETCH FIRST 3 ROWS ONLY- Hide quoted text -

- Show quoted text -
I think this will work. I tried some similar variations that I found
holes in but so far this one seems to work in all cases. I knew I was
just missing the right combo..thanks.
>Not sure I fully understand but would something along the lines of:

select col1, col2, col3 from (
select COL1, COL2, COL3, row_number() over (
order by COL1, COL2 ) as rn
from TABLE1
where COL3 = 1 and COL1 between 200 and 400
) X where rn between 0 and 3

do? rn is the *cursor* position, so next time you run the query it
would be:
...
) X where rn between 4 and 7
Lennart, Thanks for the info. I dont think this will work well in
this case since I would have to keep track of the cursor position in a
global and there would be multiple similar calls...each having it's
own cursor position. The initial caller stores the info in it's own
database so it can easily find and return the last information of COL1
and COL2. However, for them to return to me a the last cursor
position would require a lot of changes between the database and the
messaging components so I dont think they will go for it.

Mar 8 '07 #4

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype,...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: id ------------ name
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: ----------------
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: ----------------
3
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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.