28,409
社区成员




create table tb(序号 int,货号 int,状况 nvarchar(10),尺寸 varchar(10),数量 varchar(10))
insert into tb select 1,101,'未处理','S|M','2|1'
insert into tb select 2,102,'未处理','M|XL','1|1'
insert into tb select 3,102,'已处理','M','4'
insert into tb select 4,103,'未处理','L|XL','6|1'
insert into tb select 5,101,'未处理','M|L','3|1'
insert into tb select 6,103,'已处理','XL','4'
insert into tb select 7,103,'未处理','M','2'
insert into tb select 8,102,'未处理','M','1'
select 货号,状况,尺寸,sum(convert(int,数量)) as 数量 from (
select * from tb where charindex('|',尺寸)=0
union all
select 序号,货号,状况,left(尺寸,charindex('|',尺寸)-1),left(数量,charindex('|',数量)-1) from tb where charindex('|',尺寸)>0
union all
select 序号,货号,状况,right(尺寸,len(尺寸)-charindex('|',尺寸)),right(数量,len(数量)-charindex('|',数量)) from tb where charindex('|',尺寸)>0
)T group by 货号,状况,尺寸
go
drop table tb
/*
货号 状况 尺寸 数量
----------- ---------- ---------- -----------
101 未处理 L 1
101 未处理 M 4
101 未处理 S 2
102 未处理 M 2
102 未处理 XL 1
102 已处理 M 4
103 未处理 L 6
103 未处理 M 2
103 未处理 XL 1
103 已处理 XL 4
(10 行受影响)*/