从SQL中的一个left属性获取多个属性

时间:2013-11-19 09:22:47

标签: sql oracle oracle11g relational-database

如果我有这样的表:

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

我可以使用PL / SQL查询获得这样的结果吗?

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

注意UserNames后面是否有空白单元格?对不起,如果问题不是很清楚。

我认为GROUP BY查询可行,但我不确定。

2 个答案:

答案 0 :(得分:1)

请尝试:

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

SQL Fiddle Demo

答案 1 :(得分:0)

SELECT
    UserName,
    BoardName
FROM
    TheTable
GROUP BY 
    UserName,
    BoardName

会得到你

John    Cooking
John    Travel
Tom     Cooking
Bill    Sport

所以诀窍是找出组中的行号并使用

也许是这样的?

SELECT
    CASE
       WHEN ROW_NUMBER() OVER (partition by GROUP order by UserName,  BoardName) > 1 THEN ''
       ELSE UserName
    END AS UserName,   
    BoardName
FROM
    TheTable
GROUP BY 
    CASE
       WHEN ROW_NUMBER() OVER (partition by GROUP order by UserName,  BoardName) > 1 THEN ''
       ELSE UserName
    END,   
    BoardName

我没有oracle所以无法测试它。