1

我有一个查询,它为一个场所拉入团体销售订单。我当前的查询提取了我需要的所有信息,但为每个订单号提供了多行:

ordCode cltClientName          adrCityDesc  adrStateDesc    adrZipCode  gsnValue2   evShow  shDescr
739802  Stevens Elementary,    Aberdeen         WA             98520       64        183    Group - Museum Admission
739802  Stevens Elementary,    Aberdeen         WA             98520       64        237    Extreme Planets

我想看到的是每个订单都有一行,但最后一列的结果用逗号分隔:

ordCode cltClientName          adrCityDesc  adrStateDesc    adrZipCode  gsnValue2   evShow  shDescr
739802  Stevens Elementary,    Aberdeen         WA             98520       64        183    Group - Museum Admission, Extreme Planets

查询:

SELECT DISTINCT(ordCode),
        cltClientName, 
        adrCityDesc,
        adrStateDesc,
        adrZipCode,
        gsnValue2,
        evShow,
        shDescr

--COUNT(tiCode)
  from Orders 
        inner join OrderContacts on orcOrderNumber = ordCode
        inner join Clients on orcClientID = cltCode
        inner join ClientAddresses on cltCode = claClientCode
        inner join Addresses on claAddressCode = adrCode
        inner join Tickets on tiOrder = ordCode
        inner join dbo.Events on tiEvent = evCode
        inner join GroupSaleNotes on ordCode = gsnOrderNumber
        inner join Shows on evShow = shCode
 WHERE ordOrderTypeTitle = 7 -- Group Sales only
  and ordOpenDate between '2011-06-01 00:00:00.000' and '2012-05-31 23:59:59.999'
  and gsnNoteType = '5'
  and cltIsGroupLeader = '1'
   --and evShow in (106, 107)
 GROUP BY ordCode, cltClientName, cltCode, adrCityDesc, adrStateDesc,
      adrZipCode, gsnValue2, evShow, shDescr
ORDER BY adrCityDesc, cltClientName

有没有办法组合这部分结果集?

4

1 回答 1

2

您以前有一个用 MySQL 标记的问题,因此 MySQL 的解决方案将使用GROUP_CONCAT()

SELECT ordCode,
  cltClientName, 
  adrCityDesc,
  adrStateDesc,
  adrZipCode,
  gsnValue2,
  group_concat(evShow),
  group_concat(shDescr)
from Orders 
inner join OrderContacts 
  on orcOrderNumber = ordCode
inner join Clients 
  on orcClientID = cltCode
inner join ClientAddresses 
  on cltCode = claClientCode
inner join Addresses 
  on claAddressCode = adrCode
inner join Tickets 
  on tiOrder = ordCode
inner join dbo.Events 
  on tiEvent = evCode
inner join GroupSaleNotes 
  on ordCode = gsnOrderNumber
inner join Shows 
  on evShow = shCode
WHERE
  ordOrderTypeTitle = 7 -- Group Sales only
  and ordOpenDate between '2011-06-01 00:00:00.000' and '2012-05-31 23:59:59.999'
  and gsnNoteType = '5'
  and cltIsGroupLeader = '1'
  --and evShow in (106, 107)
GROUP BY ordCode,
  cltClientName, 
  cltCode,
  adrCityDesc,
  adrStateDesc,
  adrZipCode,
  gsnValue2
ORDER BY adrCityDesc,
  cltClientName

这适用于GROUP_CONCAT()theevShowshDescr列,因为这两个值都有多个值。

在 SQL Server 中,您可以使用与此类似的内容(未经测试):

SELECT ordCode,
  cltClientName, 
  adrCityDesc,
  adrStateDesc,
  adrZipCode,
  gsnValue2,
  stuff((select distinct ', ' + cast(e.evShow as varchar(10))
         from dbo.events e
         where tiEvent = evCode
         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') evShow,
  stuff((select distinct ', ' + cast(e.shDescr as varchar(10))
         from dbo.events e
         where tiEvent = evCode
         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') evShow,
from Orders 
inner join OrderContacts 
  on orcOrderNumber = ordCode
inner join Clients 
  on orcClientID = cltCode
inner join ClientAddresses 
  on cltCode = claClientCode
inner join Addresses 
  on claAddressCode = adrCode
inner join Tickets 
  on tiOrder = ordCode
--inner join dbo.Events 
--  on tiEvent = evCode
inner join GroupSaleNotes 
  on ordCode = gsnOrderNumber
inner join Shows 
  on evShow = shCode
WHERE
  ordOrderTypeTitle = 7 -- Group Sales only
  and ordOpenDate between '2011-06-01 00:00:00.000' and '2012-05-31 23:59:59.999'
  and gsnNoteType = '5'
  and cltIsGroupLeader = '1'
  --and evShow in (106, 107)
GROUP BY ordCode,
  cltClientName, 
  cltCode,
  adrCityDesc,
  adrStateDesc,
  adrZipCode,
  gsnValue2
ORDER BY adrCityDesc,
  cltClientName
于 2012-10-22T22:09:59.743 回答