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

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
AliencenetAsked:
Who is Participating?
 
slightwv (䄆 Netminder)Connect With a Mentor Commented:
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
 
AliencenetAuthor Commented:
Thanks slightwv. This helps. just missing "over" in the statement
0
 
slightwv (䄆 Netminder) Commented:
>>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
All Courses

From novice to tech pro — start learning today.