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

  • Home
  • SEARCH
  • 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 765857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:57:12+00:00 2026-05-14T16:57:12+00:00

i got this Table: CREATE TABLE Test_Table ( old_val VARCHAR2(3), new_val VARCHAR2(3), Updflag NUMBER,

  • 0

i got this Table:

CREATE TABLE Test_Table (
    old_val VARCHAR2(3),
    new_val VARCHAR2(3),
    Updflag NUMBER,
    WorkNo NUMBER  );

and this is in my Table:

INSERT INTO Test_Table  (old_val, new_val, Updflag , WorkNo) VALUES('1',' 20',0,0);
INSERT INTO Test_Table  (old_val, new_val, Updflag , WorkNo) VALUES('2',' 20',0,0);
INSERT INTO Test_Table  (old_val, new_val, Updflag , WorkNo) VALUES('2',' 30',0,0);
INSERT INTO Test_Table  (old_val, new_val, Updflag , WorkNo) VALUES('3',' 30',0,0);    
INSERT INTO Test_Table  (old_val, new_val, Updflag , WorkNo) VALUES('4',' 40',0,0);
INSERT INTO Test_Table  (old_val, new_val, Updflag , WorkNo) VALUES('4',' 40',0,0);

now my Table Looks like this:

Row  Old_val  New_val       Updflag  WorkNo
1    '1'        ' 20'       0        0
2    '2'        ' 20'       0        0
3    '2'        ' 30'       0        0
4    '3'        ' 30'       0        0
5    '4'        ' 40'       0        0
6    '5'        ' 40'       0        0

(if the value in the new_val column are same then they are together and the same goes to old_val)
so in the example above row 1-4 are together and row 5-6

at the moment i have in my Stored Procedure a cursor:

 SELECT t1.Old_val, t1.New_val, t1.updflag, t1.WorkNo
    FROM Test_Table t1
    WHERE t1.New_val =
      (
        SELECT t2.New_val
        FROM Test_Table t2
        WHERE t2.Updflag = 0
          AND t2.Worknr = 0
          AND ROWNUM = 1
      )

the output is this:

Row  Old_val  New_val   Updflag  WorkNo
1    1         20       0        0
2    2         20       0        0

my Problem is, i dont know how to get row 1 to 4 with one select.
(i had an idea with 4 sub-querys but this wont work if its more data that matches together)

does anyone of you have an idea?

  • 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-05-14T16:57:12+00:00Added an answer on May 14, 2026 at 4:57 pm

    you can use analytics to define groups of contiguous rows:

    SQL> SELECT old_val, new_val, updflag, workno,
      2         SUM(gap) over(ORDER BY old_val, new_val) grp
      3    FROM (SELECT t.*,
      4                  CASE
      5                     WHEN new_val = lag(new_val)
      6                                    over(ORDER BY old_val, new_val)
      7                       OR old_val = lag(old_val)
      8                                    over(ORDER BY old_val, new_val)
      9                     THEN
     10                      0
     11                     ELSE
     12                      1
     13                  END gap
     14             FROM Test_Table t);
    
    OLD_VAL NEW_VAL    UPDFLAG     WORKNO        GRP
    ------- ------- ---------- ---------- ----------
    1        20              0          0          1
    2        20              0          0          1
    2        30              0          0          1
    3        30              0          0          1
    4        40              0          0          2
    4        40              0          0          2
    

    The inner SELECT builds a “GAP” column that equals 1 when the current row is not in the same group as the preceeding.

    The outer SELECT uses a running total over the gap column to get the group number.

    Edit 2

    You can’t add the FOR UPDATE clause directly to the query because of the analytic functions. You can query the base table directly however:

    SQL> WITH t_new AS (
      2  SELECT t_rowid, old_val, new_val, updflag, workno,
      3         SUM(gap) over(ORDER BY old_val, new_val) grp
      4    FROM (SELECT t.*, t.rowid t_rowid,
      5                  CASE
      6                     WHEN new_val = lag(new_val)
      7                                    over(ORDER BY old_val, new_val)
      8                       OR old_val = lag(old_val)
      9                                    over(ORDER BY old_val, new_val)
     10                     THEN
     11                      0
     12                     ELSE
     13                      1
     14                  END gap
     15             FROM test_table t)
     16  )
     17  SELECT *
     18    FROM test_table
     19   WHERE ROWID IN (SELECT t_rowid
     20                     FROM t_new
     21                    WHERE grp = (SELECT grp
     22                                   FROM t_new t2
     23                                  WHERE t2.new_val = ' 20'
     24                                    AND t2.old_val = '1'))
     25     FOR UPDATE;
    
    OLD_VAL NEW_VAL    UPDFLAG     WORKNO
    ------- ------- ---------- ----------
    1        20              0          0
    2        20              0          0
    2        30              0          0
    3        30              0          0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.