Solved

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

Posted on 2011-03-09
3
294 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
  • 2
3 Comments
 
LVL 76

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 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now