View previous topic :: View next topic
|
Author |
Message |
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi,
I am working on converting a IMSDB call to DB2 call for a new DB2 DB. Earlier the logic was to issue a GU call to position the pointer in IMSDB and then issue GN calls to fetch the records (The GU call is a qualified one). Now I am using a cursor to get the records and I filter all the records in COBOL until that particular record is fetched. I could not give this particular field in the where clause as the column is not sorted (eventhough the field is in the index).I went through Scrollabe cursors but I could not get a logic to position the pointer depending on a field value. How to replace the same logic in DB2 i.e. positioning the pointer at a particular position and retrieve the values from that point?
Thanks,
Bala |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
You might consider using an ORDER BY in the select for your cursor. . .
This would allow you to see the rows in a guaranteed/predictable sequence and when you reach the position you want, process as needed from that point. |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
Quote: |
I could not give this particular field in the where clause as the column is not sorted (eventhough the field is in the index). |
What does that have to do with using the column in the where clause? |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
If I was able to give it in the where clause, I would have got the required row first. Now the required row may be after around 1000 records or more, so I want to position the pointer there so that I can start my processing from there instead of doing a cobol filter. So how to accomplish the positioning of pointer depending upon the value of a field? |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
dick scherrer wrote: |
when you reach the position you want, process as needed om that point. |
Hi Dick,
I am searhing for a solution to reach the required position. Do you mean that I should have a loop in my cobol program until I reach that point? |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
nbalajibe wrote: |
If I was able to give it in the where clause, I would have got the required row first. Now the required row may be after around 1000 records or more, so I want to position the pointer there so that I can start my processing from there instead of doing a cobol filter. So how to accomplish the positioning of pointer depending upon the value of a field? |
My question is why can you not use the field in a where cluase? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello Shyam,
We do not yet understand your situation.
I would think that either using the WHERE or by using a cursor which will return rows in a predictable order (and allowing multiple rows with the cursor), you would be able to retrieve the row(s) you want and exclude others.
Maybe if you post sample data, we will better able to understand. |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi,
Let me explain with a example. Consider the DB containing the Date of Joining of the students, their name and their ranks. The DB is sorted in the descending order of the DOJ of the students. If I were to retrieve the rows below a particular date and starting from a student's rank, I cannot give "where date <= '2007-10-01' and where rank => '5' ".
In this case if a rank at the next date is less than 5 that would not get retrieved. Also we need the output in the desc order of date, hence we cannot sort in the order of rank. Hence I am not having rank in the where clause and putting a loop in my program till I reach that particular students rank but I want to remove this unnecessary loop. Hope this explain my issue. |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi Dick,
Let us consider the DB contents as follows,
DOJ Name Rank
10/1/2007 rrr 50
9/9/2007 xxx 5
9/9/2007 yyy 10
9/9/2007 ccc 15
8/7/2007 ddd 2
8/7/2007 eee 25
8/7/2007 zzz 35
7/6/2007 aaa 1
5/5/2007 bbb 5
and the expected result is,
DOJ Name Rank
9/9/2007 yyy 10
9/9/2007 ccc 15
8/7/2007 ddd 2
8/7/2007 eee 25
8/7/2007 zzz 35
7/6/2007 aaa 1
5/5/2007 bbb 5
If there are around 10000 students who joined on that particular date. I have retrieved the details of 9000 students earlier and now I want only the details of 1000 students also the identifier here is the rank which is in the sorted order inside a date, so I open the cursor here and do a loop until I reach that 9001 record (In real situation the record number is unknown). But I want to reach to that particular record instead of scanning all the 9000 records. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
I cannot give "where date <= '2007-10-01' and where rank => '5' ".
In this case if a rank at the next date is less than 5 that would not get retrieved |
Unless there is a typo or i still misunderstand, rows with a rank less than 5 are not supposed to be selected?
If you try something like
Code: |
select DOJ, name, rank
from tabl where DOJ <= '2007-10-01'
and rank => '5'
order by DOJ desc, rank |
what are the results? |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
dick scherrer wrote: |
select DOJ, name, rank
from tabl where DOJ <= '2007-10-01'
and rank => '5'
order by DOJ desc, rank[/code]what are the results? |
Hi Dick,
I need all the records below that particular entry, hence i need a row even if the rank is less than 5 in the next rows. The query given does not retrieve the rank below 5 in the next date, because the fields are first ordered by date and then by rank.
Also the index is built as "date desc and rank asc" hence the order of rank within a date is known, but the ranks in the other date might have any value. Hence I am searching a logic to place the pointer at a particular row (the value of which is known) as I have mentioned earlier (a call similar to a GU in IMS to position the pointer). |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hi Shyam,
Yes, i misunderstood - again Possibly, i better understand now. . .
I will look around and try to find something more suitable to what you need.
How do you feel about a temporary table or some sort of 2-phase approach (1 to determine the place to start and the other to return all rows below that point)? I'm not necessarily recommending these, just wondering what your thoughts are.
How often will this process be run? The frequency may impact the approach.
One other thing to consider is to unload the table and do what you want with a qsam file. . . Not always is database the better solution. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
I mildly suggest that You review Your understanding of how a relational DB works...
a relational database/table is LOGICALLY NEVER sorted on anything...
all the INDEXes You might define are there mainly for performance reasons..
( forget for a moment about foreign keys and unique keys )
the most important relational DB programming paradigm is..
never assume that the rows retrieved are in any specific order...
unless in Your query You specify a clause 'ORDER BY'
sometimes INDEXES are built by the the DB support team,
without application programmer' s awareness,
because of application performance tuning |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
Quote: |
Let us consider the DB contents as follows,
DOJ Name Rank
10/1/2007 rrr 50
9/9/2007 xxx 5
9/9/2007 yyy 10
9/9/2007 ccc 15
8/7/2007 ddd 2
8/7/2007 eee 25
8/7/2007 zzz 35
7/6/2007 aaa 1
5/5/2007 bbb 5
and the expected result is,
DOJ Name Rank
9/9/2007 yyy 10
9/9/2007 ccc 15
8/7/2007 ddd 2
8/7/2007 eee 25
8/7/2007 zzz 35
7/6/2007 aaa 1
5/5/2007 bbb 5 |
Code: |
select DOJ, name, rank
from tabl where DOJ <= '2007-10-01'
order by DOJ desc, rank |
the above query will give exactly Your ouput,
but please explain why 9/9/2007 xxx 5
was not in the output ( maybe a typo )
using a cursor will let You scan the records selected and take any
action You like.. |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi Dick,
Would the idea of temp table or sort improve the performance of the system, I am looking for a new logic to eliminate cobol filter logic.
This process would be called from 60% of the jobs and onlines daily (hence performance should be high).
Would there be any logic in JOINS or Subqueries to satisfy the condition? |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi E.S.,
Thanks for making things clear.
As per my requirement only the rows starting from the given record should be fetched, hence 9/9/2007 xxx 5 was not which is above the given record should not be retrieved (the starting record given here is 9/9/2007 yyy 10). In this case we are having only one record above the starting one, in the worst case there can be thousands of records for the same date, but i need to retrieve only the records below the given record. Currently I am using the query you sent but I have to filter all the records above the required record, I want to remove the filtering logic. |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
nbalajibe wrote: |
Hi E.S.,
Thanks for making things clear.
As per my requirement only the rows starting from the given record should be fetched, hence 9/9/2007 xxx 5 which is above the given record should not be retrieved (the starting record given here is 9/9/2007 yyy 10). In this case we are having only one record above the starting one, in the worst case there can be thousands of records for the same date, but i need to retrieve only the records below the given record. Currently I am using the query you sent but I have to filter all the records above the required record, I want to remove the filtering logic. |
I have corrected the typing error of the previous post. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
we' ll have to elucubrate a little,
maybe the donkey approach of selecting first the good date
something like ( forget the syntax, it' s just to grasp the logic )
select date from ... where date < some_date
order by date desc fetch first row only
and You get the first_good_date to process
select all_you_need from ....
where date < first_good_date or
( date = first_good_date and rank > first_rank )
order by date desc, rank
and You get all You need ...
the logic seems good, I do not know about the performance |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi,
I have no issues in positioning the pointer at the required date, my issues are only with positioning the pointer at the required rank within it. Also I need all the rows below that (this includes the rows with other dates below this too). |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
Have You looked at my select sketches.. ?
the first one positions to the date,
the second retrieves exactly what You need.. OR NOT ??? :-)
the first query will retrieve 9/9/2007
( the first date lower then 10/1/2007)
the second one one splits the where clause in two
selecting date less then 9/9/2007 regardless of the rank
and for 9/9/2007 selects the rows with a rank greater then 5
I tend to believe that it should work |
|
Back to top |
|
|
nbalajibe Warnings : 1 New User
Joined: 28 Nov 2006 Posts: 75 Location: India
|
|
|
|
Hi E.S.,
Thanks for the query. I tried this and it works fine. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Good to hear it is now working
Thank you for posting the good news
d |
|
Back to top |
|
|
|