22,209
社区成员
发帖
与我相关
我的任务
分享
--3.合并字符串
create table test4 (name varchar(10),mytype varchar(10),cj int )
insert into test4
values('张三','语文',83),
('张三','数学',65),
('张三','物理',85),
('李四','语文',73),
('李四','数学',69),
('李四','物理',93)
select name,
stuff((select ','+mytype from test4 t4 where t4.name = test4.name for xml path('')),1,1,'') as mytype,
stuff((select ','+cast(cj as varchar) from test4 t4 where t4.name = test4.name for xml path('')),1,1,'') as cj
from test4
group by name
/*
name mytype cj
李四 语文,数学,物理 73,69,93
张三 语文,数学,物理 83,65,85
*/
另外,你要合并产品,用逗号分隔,可以用xml,写个子查询,或者写个自定义函数
大致上是这样的:select p.fname,p.lname... from Customers c inner join party p on c.PartyID = p.PartyID cross apply ( select v.* from ( select top 1 o.*,sum(o.PaidAmount) over(partition by OrderID) as sumPaidAmount from orders o where o.PartyID = c.PartyID and o.PaidAmount >= 1000 order by OrderDate desc )v where v.sumPaidAmount >= 1000 )o inner join products ps on ps.ProdType =o.ProdType where c.EmailAddress_Contact like '%@gmail.com'
select p.fname,p.lname...
from Customers c
inner join party p
on c.PartyID = p.PartyID
cross apply
(
select v.*
from
(
select top 1 o.*,sum(o.PaidAmount) over(partition by OrderID) as sumPaidAmount
from orders o
where o.PartyID = c.PartyID
and o.PaidAmount >= 1000
order by OrderDate desc
)v
where v.sumPaidAmount >= 1000
)o
inner join products ps
on ps.ProdType =o.ProdType
where c.EmailAddress_Contact like '%@gmail.com'