在SQL中连接行

时间:2013-07-04 05:34:32

标签: sql database oracle

我有一个名为Dummy的表,如下所示:

No.   Name
1     ABC
2     NMD
2     SDSDS
1     23ererer

现在我希望将所有Name列连接到一个给定的数字

对于前。说号码是1我希望ABC23ererer作为我的输出

这是在ORACLE(SQL)中完成的,不使用PL-SQL。

如何做到这一点?

4 个答案:

答案 0 :(得分:1)

这可能会有所帮助......

select NO,
  listagg(NAME, ',') within group (order by NAME) as name
from TableName
group by NO

或者检查this

答案 1 :(得分:1)

  

oracle10g不支持LISTAGG。如果您有10g我认为以下查询将帮助您。

select No, rtrim(Name,',') Name
       from ( select No
      , Name, rn
         from yourtable
               model
                      partition by (No)
                        dimension by (row_number() over
                                     (partition by No order by Name) rn
                                    )
                       measures     (cast(Name as varchar2(40)) Name)
                       rules
                       ( Name[any] order by rn desc = Name[cv()]||''||Name[cv()+1]
                       )
              )
        where rn = 1
        order by NO

查看您的demo in sql fiddle

答案 2 :(得分:0)

SELECT "No.", LISTAGG(Name, ',') WITHIN GROUP (ORDER BY "No.") AS Name
FROM TableName
GROUP BY "No.";

答案 3 :(得分:-4)

试试这个:

SELECT Number+' '+
Name FROM table_name