Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-09
3
Medium Priority
?
310 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to recover a database from a user managed backup

704 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