If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Compare previous record in db2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-11, 06:22
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
Compare previous record in db2

Hello,
Need some quick help..

I need to fetch the previous record and the current record ( in predicate ) in a single query of DB2...

Can somebody help me how it can be done!!!

Thanks for your help !!!

Thanks
Thamin
Reply With Quote
  #2 (permalink)  
Old 07-15-11, 06:31
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
You need to provide a lot more information on what you are trying to do. There is no such thing as "previous record" in DB2.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 07-15-11, 06:33
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Can you give an example? Also useful will be the background of the requirement



--
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 07-15-11, 06:41
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
For Example :

I have a table TAB1 like this

Name dept exp
AAA 123 4
DDD 321 6
KKK 456 7
LLL 555 5
SSS 888 5

Now I have a select statement
SELECT NAME FROM TAB1 WHERE NAME='LLL'

I need to retrieve both LLL and KKK record

Thanks,
Thamizh
Reply With Quote
  #5 (permalink)  
Old 07-15-11, 07:53
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
As Marcus mentioned earlier, there is no concept of previous record in DB2. You have to use some logic to say what a previous record is ...
For eg, in your sample data, it could be order by on Name column . you can use row number to get the records in this case or even
select a.* from tab1 a where name<='LLL' order by name desc fetch first 2 rows only

HTH

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 07-15-11, 09:36
shubin_du shubin_du is offline
Registered User
 
Join Date: Feb 2009
Location: Germany
Posts: 23
you can use the lag function to get the fields from the previous statement

Code:
lag( name) over( order by field_list )
you can find examples in docu "SQL Reference 1 ".

But your select has no previous statements because

WHERE NAME='LLL'

finds only 1 record according to your example. If your result set has more than 1 record you can use lag for previous and lead for the next record in the record set.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On