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 566465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:58:49+00:00 2026-05-13T12:58:49+00:00

I have a table which represents a line-by-line dump of the data read from

  • 0

I have a table which represents a line-by-line dump of the data read from a particular text file format. Each line may represent a “master” or a “detail” line, indicated via rec_type code. I’d like to write a query that gets the “master” lines alongside the associated detail lines. I’ve come up with something that does the job, but it seems a bit hackish and am interested in better ways if any.

CREATE TABLE mdtest
 (rec_seq  NUMBER        PRIMARY KEY
 ,rec_type VARCHAR2(3)   NOT NULL
 ,rec_data VARCHAR2(100) NOT NULL);

INSERT INTO mdtest VALUES (1, '100', 'Bill Jones');
INSERT INTO mdtest VALUES (2, '200', '20080115,100.25');
INSERT INTO mdtest VALUES (3, '100', 'John Smith');
INSERT INTO mdtest VALUES (4, '200', '20090701,80.95');
INSERT INTO mdtest VALUES (5, '200', '20091231,110.35');

Desired Result:

SEQ_EMP  EMP_NAME    SEQ_DATA  EMP_DATA
=======  ==========  ========  ===============
      1  Bill Jones         2  20080115,100.25
      3  John Smith         4  20090701,80.95
      3  John Smith         5  20091231,110.35

Assumptions:

  • records are processed in sequence of rec_seq
  • first record type is a “100“
  • each “100” record has 1 or more “200” records following

Note: this is for Oracle 9i, however we should be upgrading to 11g R1 this year.

  • 1 1 Answer
  • 1 View
  • 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-13T12:58:49+00:00Added an answer on May 13, 2026 at 12:58 pm

    Here’s what I have so far:

    SELECT seq_emp 
          ,SUBSTR(emp_seq_name,10) emp_name 
          ,seq_data 
          ,emp_data 
    FROM  (SELECT MAX(CASE WHEN rec_type = '100' THEN rec_seq END) 
                  OVER (ORDER BY rec_seq 
                        ROWS BETWEEN UNBOUNDED PRECEDING 
                                 AND CURRENT ROW) seq_emp 
                 ,MAX(CASE 
                      WHEN rec_type = '100' 
                      THEN TO_CHAR(rec_seq,'fm00000000') || '|' || rec_data 
                      END) 
                  OVER (ORDER BY rec_seq 
                        ROWS BETWEEN UNBOUNDED PRECEDING 
                                 AND CURRENT ROW) emp_seq_name 
                 ,rec_seq seq_data 
                 ,rec_type 
                 ,rec_data emp_data 
           FROM   mdtest) 
    WHERE  rec_type = '200' 
    ORDER BY seq_data; 
    

    As you can see, I’m using the MAX reporting analytic function with a window starting from the top of the set down to the current row, to get the relevant “100” record for the current “200” record; then in the outer query I’m discarding the unneeded “100” records.

    To get emp_name, I’ve had to append the rec_seq with the data so that the MAX function still picks the correct header record; then in the outer query I chop the rec_seq off.

    I’ve played with other analytic functions and syntax including FIRST_VALUE and the KEEP syntax, but neither of these seem to make this job simpler; the difficulty is that the window is defined by the value of rec_type instead of being some constant offset.

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

Sidebar

Related Questions

I have a html table in which each line ( <tr> ) represents a
I have a table of data which represents a series of events that persons
I have a text file containing data/fields which are separated by exact column no.
in my jqgrid i have field in a table which represents the id and
I have a table which has two text boxes and an image beside it
I have table called page which represents every single page in my website. page_id
I have a mySQL table which represents a company's products. The table shows whether
I have a friends table: id , fid1 , fid2 which represents a friendship
I receive data in a CSV (kind of) format, in which each entity is
I have a table which represents a Contract between two rows in another table.

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.