SQL列表项不重复第一个

时间:2016-05-02 17:19:53

标签: mysql sql oracle

我有一个包含赋值名称的表 例如:

contact_task#   ContactName
984             Michael  
984             Andrew 
984             Steve
984             Paul

我想基于上面的内容创建一个查询,以便我得到类似这样的内容

Contact_task#   ContactName   OtherContact
984             Michael       Andrew, Steve, Paul

984             Andrew        Michael, Steve, Paul

984             Steve         Michael, Andrew, Paul

984             PAul          Michael, Andrew, Steve

我已经开始了,但我无法将contactName重新显示在其他联系人

select a.Contact_task, a.ContactName, OtherContact
from contacttable a

 LEFT OUTER JOIN (SELECT xx.Contact_task,
                          rtrim(MAX(decode(rn,
                                           1,
                                           xx.ContactName)) || ', ' ||
                                MAX(decode(rn,
                                           2,
                                           xx.ContactName)) || ', ' ||
                                MAX(decode(rn,
                                           3,
                                           xx.ContactName)) || ', ' ||
                                MAX(decode(rn,
                                           4,
                                           xx.ContactName)) || ', ' ||
                                MAX(decode(rn,
                                           5,
                                           xx.ContactName)) || ', ' ||
                                MAX(decode(rn,
                                           6,
                                           xx.ContactName)),
                                ', ') AS OtherContact
                     FROM (SELECT a.Contact_task,
                               ContactName,
                                  row_number() over(PARTITION BY a.Contact_task ORDER BY ContactName ) rn
                             FROM contacttable a

                          ) xx
                    GROUP BY xx.Contact_task) y ON a.Contact_task =
                                                  y.Contact_task


where a.Contact_task = '984'

3 个答案:

答案 0 :(得分:1)

这是一种方法 -

ListView

基本上,我已经加入了表格,其条件是ID相同,但名称不是。这样,在您的示例中,WITH data AS (SELECT 984 id, 'M' name FROM DUAL UNION ALL SELECT 984 id, 'A' name FROM DUAL UNION ALL SELECT 984 id, 'S' name FROM DUAL UNION ALL SELECT 984 id, 'P' name FROM DUAL) SELECT d1.id, d1.name, LISTAGG (d2.name, ',') WITHIN GROUP (ORDER BY d2.name) other FROM data d1 JOIN data d2 ON d1.id = d2.id AND d1.name <> d2.name GROUP BY d1.id, d1.name; 将基于相同的ID和不同的名称加入3行 - ID:984, Name:Michael

然后您需要做的就是Andrew,Steve,and Paul和其他名称上的GROUP BY

答案 1 :(得分:1)

在Oracle中不确定MySQL ...

function () {
    fn_UpdateSortColumns.call(this);

<强>输出

with assignment_names (contact_task#, ContactName) as
   ( select 984, 'Michael' from dual union all
     select 984, 'Andrew'  from dual union all
     select 984, 'Steve'   from dual union all
     select 984, 'Paul'    from dual union all
     select 990, 'Mary'    from dual union all
     select 990, 'Joseph'  from dual)
select contact_task#, 
       ContactName, 
       (select listagg(contactName, ', ') within group (order by ContactName)
        from assignment_names a1
        where a1.contact_task# = a.contact_task# and a1.ContactName != a.ContactName
        group by a1.contact_task#) as OtherContacts
from assignment_names a
/

答案 2 :(得分:0)

我不确定你为什么要这样做,但无论如何......

SELECT x.*
     , GROUP_CONCAT(y.contactname) others 
  FROM my_table x 
  JOIN my_table y 
    ON y.contact_task = x.contact_task 
   AND y.contactname <> x.contactname 
 GROUP  
    BY contact_task,contactname;