Solved

Oracle SQL query to identify records that belong to the same primary record.

Posted on 2011-03-09
3
306 Views
Last Modified: 2012-05-11
I have a table with the following data:
ID             TYPE
0100001         A
0100001         B
0100001         C
0100002     A
0100002     C

I'm looking to number records base on their ID so I can then create one record per ID.
TYPE column shouldn't be the driving factor in numbering records but rather change in the ID number. So first step is to number the records with the same ID:
Output:
ID             TYPE    POSITION
0100001         A             1
0100001         B             2
0100001         C             3
0100002     A             1
0100002     C             2

After I have records numbered I can use the following to create one record per ID.
select id,type,position (subquery where id=id and position=2),(subquery where id=id and position=3) from my_table where position=1;

ID             TYPE    POSITION    SUBQUERY1          SUBQUERY2
0100001         A             1                   B                             C
0100002     A             1                   C
0
Comment
Question by:Aliencenet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 35089620
The number is easy:
Select Id, type, position
From
(
Select id, type, row_number() (partition by id order by id,type) position
From table
)
Order by id, position
/

The pivot is the hard part.  In Oracle you need to know the number of columns.

If you can go with a CSV, it get's easier.
0
 

Author Comment

by:Aliencenet
ID: 35104921
Thanks slightwv. This helps. just missing "over" in the statement
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35108862
>>just missing "over" in the statement

lol.. yes I did.

Typing all that in on a Mobile device, I'm surprised that's all I missed.

Glad to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
capture vmstat info and insert it into an oracle table 31 93
SQL Syntax Question 9 57
Oracle Date add 9 36
Query for non-UTF8 characters in a column in Oracle 11 42
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question