Link to home
Create AccountLog in
Avatar of Aliencenet
Aliencenet

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Aliencenet
Aliencenet

ASKER

Thanks slightwv. This helps. just missing "over" in the statement
>>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.