Solved

How to turn multiple rows into one row in oracle without creating functions or views

Posted on 2014-03-14
3
599 Views
Last Modified: 2014-03-14
In my database, I have e.g following 2 rows

SSSN                           UNIT           INDV     PROF
330511-000      GYMBER01      INDV          NULL      
330511-000      GYMBER01      NULL          PROF

I want it displayed like this, that is: only one row

SSSN                           UNIT             INDV              PROF
330511-000      GYMBER01        INDV             PROF      




I know how to achieve this by Writing functions or multiple views which I then join, but I would also be good to be able to achieve this if I only have read-rights in the db.

This is the code that returns the first to rows:

select  ssn, UNIT,

case  when kurskod='INDV' then kurskod  end AS INDV,
case  when kurskod='PROF' then kurskod  end AS PROF

from aktivitet


Open in new window

0
Comment
Question by:marcgu
3 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 450 total points
ID: 39929518
SELECT sssn,
         unit,
         MAX(CASE WHEN kurskod = 'INDV' THEN kurskod END) indv,
         MAX(CASE WHEN kurskod = 'PROF' THEN kurskod END) prof
    FROM aktivitet
GROUP BY sssn, unit
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 50 total points
ID: 39929524
The code you posted would not return two rows unless:

   1) The table contain duplicate ssn or
   2) You are either joining the table to itself or doing a UNION.

In any case you could use the MAX() function to eliminate the NULL columns.
0
 

Author Closing Comment

by:marcgu
ID: 39929541
Hi!

Great solutions. I give sdstuber most since it was posted first and complete.
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.

Question has a verified solution.

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

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…
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…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to recover a database from a user managed backup

777 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