Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get multiple attributes from one left attribute in SQL

If I have a table like this:

UserName | BoardName
-----------------------
Alice   Research
Ethan   Research
Ethan   Gardening
Ethan   Movies
Jack    Travel
Jack    Movies
Tom         Gardening
Tom         Teaching
Tom         Travel
Tom         Movies
Tom         Study

Can I get results like this using a PL/SQL query?

Alice   Research
Ethan   Research
            Gardening
            Movies
Jack    Travel
            Movies
Tom         Gardening
            Teaching
            Travel
            Movies
            Study

Notice how there are blank cells after the UserNames? Sorry if the question is not very clear.

I think a GROUP BY query would work but I am not sure.

like image 777
timewarper Avatar asked Feb 02 '26 20:02

timewarper


1 Answers

Please try:

SELECT  
     case when UserName=lag(UserName) OVER (ORDER BY UserName) 
     then null 
     else UserName end UserName,
     UserEmail, 
     BoardName
FROM 
     YourTable;

SQL Fiddle Demo

like image 82
TechDo Avatar answered Feb 05 '26 11:02

TechDo