Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:21:01+00:00 2026-06-04T22:21:01+00:00

How can I get previous or next product_id from table if I have only

  • 0

How can I get previous or next product_id from table if I have only varchar to sort?
Example if I have PRODUCT_NO='20120033' and PRODUCT_REGISTRATION_NO='yellow', I would like to get the next record PRODUCT_ID which is 4 and I have PRODUCT_NO='20120033' and PRODUCT_REGISTRATION_NO='green', I would like to get the previous record PRODUCT_ID which is 3 How can I achieve this?

Thanks

Table structure and data are pasted below

CREATE TABLE MYTAB
(
  PRODUCT_ID               NUMBER,
  PRODUCT_NO               VARCHAR2(),
  PRODUCT_REGISTRATION_NO  VARCHAR2(32)
);


SET DEFINE OFF;
Insert into MYTAB
   (PRODUCT_ID, PRODUCT_NO, PRODUCT_REGISTRATION_NO)
 Values
   (3, '20120033', 'yellow');
Insert into MYTAB
   (PRODUCT_ID, PRODUCT_NO, PRODUCT_REGISTRATION_NO)
 Values
   (4, '20120033', 'green');
Insert into MYTAB
   (PRODUCT_ID, PRODUCT_NO, PRODUCT_REGISTRATION_NO)
 Values
   (5, '20120034', 'wine');
Insert into MYTAB
   (PRODUCT_ID, PRODUCT_NO, PRODUCT_REGISTRATION_NO)
 Values
   (6, '20120034', 'soda');
COMMIT;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:21:03+00:00Added an answer on June 4, 2026 at 10:21 pm

    Use the lag() and lead() functions:

    select *
    from (
        select product_id,
               product_no,
               product_registration_no, 
               lag(product_id) over (partition by product_no order by product_id) as prev_id,
               lead(product_id) over (partition by product_no order by product_id) as next_id
        from mytab
    ) 
    where product_no='20120033' 
    and product_registration_no='green' 
    

    Note that the concept of “previous” and “next” only makes sense if you can specify a condition to sort on. That’s what the order by product_id in the window definition does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get the previous or next element from a jQuery selection? Basically,
In MySQL you can get the next or previous record based on the key/id
How can I get next element of element to which I have reference in
How can I get the previous version of data of a Row in a
I have an app with Photos. Users can view photos directly from a link
I want to have a keyboard which has a Next,Previous and Done button on
How can I find the next day's date after a given date. For example,
Is it possible to get the previous and next DST transition timestamp with the
I can't figure out how to select a previous/next row IF the current row
Possible Duplicate: Previous/next Buttons? how can i make next page and prev page in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.