【问题标题】:Group by on table, list all of one attribute for each of another attribute [duplicate]按表分组,列出另一个属性的所有一个属性[重复]
【发布时间】:2013-12-09 05:11:52
【问题描述】:

我正在尝试按名称对这些数据进行分组,而不是当前的查询输出给我:

Name                       Number
Nice guy                   1
Nice guy                   2
Nice guy                   4
Nice guy                   5
Nice guy                   6
Nice guy                   7
Nice guy                   8
Nice guy                   9
Nice guy                   10
Nice guy                   11
Nice guy                   12
Frank                      3
Frank                      4

我会得到这个:

Name       Number
Nice guy   1,2,4,...
Frank      3,4

这是我当前的查询:

select distinct name, number
from patterns,numbers,people
where patterns.index=numbers.index
AND patterns.id=people.id
order by name, charge;

我尝试过的是这样,但是失败了:

select distinct name, number
from patterns,numbers,people
where patterns.index=numbers.index
AND patterns.id=people.id
group by name
order by name, number;

任何帮助将不胜感激!

【问题讨论】:

    标签: sql oracle10g sqlplus


    【解决方案1】:

    更新:试试这个方法

    SELECT name, WM_CONCAT(number) number
      FROM
    (
      SELECT DISTINCT name, number 
        FROM patterns t JOIN numbers n
          ON t.index = n.index JOIN people p
          ON t.id = p.id
    ) q
     GROUP BY name
     ORDER BY name
    

    【讨论】:

    • 这行得通!但是,我现在通过连接得到重复值。对好人的意义我得到 1,1,2,... 而不是我之前得到的 1,2,... 有没有办法从中删除重复的值?
    • @thehandyman 查看更新的答案
    • 谢谢,我自己得到了这个,但我仍然很感激!以前没见过这个功能。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2015-02-11
    • 1970-01-01
    • 2011-05-09
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多