--> --> (Andy)生成测试数据 2008-08-20
Set Nocount On
declare @1 table([流水号] int,[企业ID] int,[订单日期] Datetime,[订单号] nvarchar(7))
Insert @1
select 1,1,'2008-01-01',N'no00001' union all
select 2,1,'2008-01-01',N'no00002' union all
select 3,1,'2008-01-01',N'no00003' union all
select 4,1,'2008-01-01',N'no00004' union all
select 5,2,'2008-01-01',N'no00005' union all
select 6,2,'2008-01-01',N'no00006'
Select [企业ID],[订单日期],[订单号],[订单数量]
From (Select [企业ID],[订单日期],[订单数量]=Count(*)
From @1
Group By [企业ID],[订单日期]) a
Outer Apply(Select [订单号]=Stuff((Select ','+[订单号]
From @1
Where [企业ID]=a.[企业ID] And [订单日期]=a.[订单日期] For Xml Path('')
),1,1,'')
) p
/*
企业ID 订单日期 订单号 订单数量
-----------------------------------------------------------------------------
1 2008-01-01 00:00:00.000 no00001,no00002,no00003,no00004 4
2 2008-01-01 00:00:00.000 no00005,no00006 2
*/