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

私が見たいのは、注文ごとに 1 つの行ですが、最後の列にはコンマで区切られた結果があります。

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()の両方に適用されます。evShowshDescr

SQL Server では、次のようなものを使用できます ( untested ):

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 に答える