如何在同一列中附加不同的值?

时间:2016-08-16 15:33:19

标签: sql-server

我有这个问题:

[select distinct pckwrk.ordnum AA,
    pckwrk.prtnum BB,
    pckwrk.pckqty CC,
    pckwrk.appqty DD,
    invdtl.rcvkey EE
from pckwrk,
     invdtl
where pckwrk.ship_line_id = invdtl.ship_line_id
    and pckwrk.wrkref = invdtl.wrkref
    and pckwrk.prtnum = invdtl.prtnum
    and pckwrk.wh_id = 'MFTZ'
    and pckwrk.prt_client_id = 'HUS'
    and invdtl.prt_client_id = 'HUS'
    and pckwrk.ordnum = '85684780'
    and pckwrk.prtnum = '1103329'
group by pckwrk.ordnum,
      pckwrk.prtnum,
      pckwrk.pckqty,
      pckwrk.appqty,
      invdtl.rcvkey]

显示以下结果:

 aa            bb      cc   dd   ee
85684780     1103329    3   3   150900000164043
85684780     1103329    4   4   150900000164043
85684780     1103329    4   4   150900000164065
85684780     1103329    6   6   151200000170364

我可以做什么来合并第3行和第4行,因为它们是相同的,并按如下方式显示列EE:

  aa            bb     cc   dd   ee
85684780     1103329    3   3   150900000164043
85684780     1103329    4   4   150900000164043,150900000164065
85684780     1103329    6   6   151200000170364

非常感谢您的支持!!

这是我正在使用的代码,它给出了错误消息:状态:511-Database Error 511-'cast'附近的语法不正确。

[SELECT y.FF,
    STUFF((SELECT ', ' + y.II
             FROM (select distinct pckwrk.ordnum EE,
                          pckwrk.prtnum FF,
                          pckwrk.pckqty GG,
                          pckwrk.appqty HH,
                          invdtl.rcvkey II
                     from pckwrk,
                          invdtl
                    where pckwrk.ship_line_id = invdtl.ship_line_id
                      and pckwrk.wrkref = invdtl.wrkref
                      and pckwrk.prtnum = invdtl.prtnum
                      and pckwrk.wh_id = 'MFTZ'
                      and pckwrk.prt_client_id = 'HUS'
                      and invdtl.prt_client_id = 'HUS'
                      and pckwrk.ordnum = '85684780'
                      and pckwrk.prtnum = '1103329'
                    group by pckwrk.ordnum,
                          pckwrk.prtnum,
                          pckwrk.pckqty,
                          pckwrk.appqty,
                          invdtl.rcvkey) y
            WHERE y.FF = x.FF FOR XML PATH(''), TYPE) .value('.[1]',' nvarchar(max)'), 1, 2, '')
FROM (select distinct pckwrk.ordnum EE,
            pckwrk.prtnum FF,
            pckwrk.pckqty GG,
            pckwrk.appqty HH,
            invdtl.rcvkey II
       from pckwrk,
            invdtl
      where pckwrk.ship_line_id = invdtl.ship_line_id
        and pckwrk.wrkref = invdtl.wrkref
        and pckwrk.prtnum = invdtl.prtnum
        and pckwrk.wh_id = 'MFTZ'
        and pckwrk.prt_client_id = 'HUS'
        and invdtl.prt_client_id = 'HUS'
        and pckwrk.ordnum = '85684780'
        and pckwrk.prtnum = '1103329'
      group by pckwrk.ordnum,
            pckwrk.prtnum,
            pckwrk.pckqty,
            pckwrk.appqty,
            invdtl.rcvkey) x
  GROUP BY y.FF]

1 个答案:

答案 0 :(得分:0)

尝试使用GROUP_CONCAT

select distinct p.ordnum AA,
p.prtnum BB,
p.pckqty CC,
p.appqty DD,
GROUP_CONCAT(i.rcvkey) EE
from pckwrk p
inner join invdtl i 
ON p.ship_line_id = i.ship_line_id
and p.wrkref = i.wrkref
and p.prtnum = i.prtnum
and p.wh_id = 'MFTZ'
and p.prt_client_id = 'HUS'
and i.prt_client_id = 'HUS'
and p.ordnum = '85684780'
and p.prtnum = '1103329'
group by p.ordnum,
  p.prtnum,
  p.pckqty,
  p.appqty