22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb') is not null
drop table tb
go
create table tb(型号 varchar(10),规格 varchar(10),净重 numeric(4,1))
insert tb select 'bopp光膜','18*600',105.1
union all select 'bopp光膜','18*600',105.2
union all select 'bopp光膜','18*600',105.3
union all select 'bopp光膜','18*600',105.4
union all select 'bopp光膜','18*600',105.5
union all select 'bopp光膜','18*600',105.6
union all select 'bopp光膜','18*600',105.7
union all select 'bopp光膜','18*600',105.8
union all select 'bopp光膜','18*600',105.4
union all select 'bopp光膜','18*600',105.6
union all select 'bopp光膜','18*600',105.1
union all select 'bopp光膜','18*600',105.2
union all select 'bopp光膜','18*610',105.1
union all select 'bopp光膜','18*610',105.1
union all select 'bopp光膜','18*610',105.2
union all select 'bopp光膜','18*620',105.1
union all select 'bopp光膜','18*620',105.4
union all select 'bopp光膜','18*800',105.1
union all select 'bopp光膜','18*800',105.8
union all select 'bopp光膜','18*800',105.1
;with test1 as
(
select id=row_number() over(partition by 型号,规格 order by getdate()),型号,规格,净重 from tb
)
,test2 as
(
select id,id1=row_number() over(partition by 型号,规格,(id-1)/8 order by getdate()),型号,规格,净重 from test1
)
,test3 as
(
select id,id1,型号,规格,净重,件数=count(*) over(partition by 型号,规格,id-id1),合计=sum(净重) over(partition by 型号,规格,id-id1) from test2
)
select distinct 型号,规格,重量明细=stuff((select ','+ltrim(净重) from test3 where 型号=t.型号 and 规格=t.规格 and id-id1=t.id-t.id1 for xml path('')),1,1,''),
件数,合计 from test3 t
型号 规格 重量明细 件数合计
bopp光膜 18*600 105.1,105.2,105.3,105.4,105.5,105.6,105.7,105.8 8 843.6
bopp光膜 18*600 105.4,105.6,105.1,105.2 4 421.3
bopp光膜 18*610 105.1,105.1,105.2 3 315.4
bopp光膜 18*620 105.1,105.4 2 210.5
bopp光膜 18*800 105.1,105.8,105.1 3 316.0
if object_id('tb') is not null
drop table tb
go
create table tb(型号 varchar(10),规格 varchar(10),净重 numeric(4,1))
insert tb select 'bopp光膜','18*600',105.1
union all select 'bopp光膜','18*600',105.2
union all select 'bopp光膜','18*600',105.3
union all select 'bopp光膜','18*600',105.4
union all select 'bopp光膜','18*600',105.5
union all select 'bopp光膜','18*600',105.6
union all select 'bopp光膜','18*600',105.7
union all select 'bopp光膜','18*600',105.8
union all select 'bopp光膜','18*600',105.4
union all select 'bopp光膜','18*600',105.6
union all select 'bopp光膜','18*600',105.1
union all select 'bopp光膜','18*600',105.2
union all select 'bopp光膜','18*610',105.1
union all select 'bopp光膜','18*610',105.1
union all select 'bopp光膜','18*610',105.2
union all select 'bopp光膜','18*620',105.1
union all select 'bopp光膜','18*620',105.4
union all select 'bopp光膜','18*800',105.1
union all select 'bopp光膜','18*800',105.8
union all select 'bopp光膜','18*800',105.1
go
with cte as
(
select rn= ROW_NUMBER()over(PARTITION by 型号,规格 order by getdate()),*
from tb
)
select 型号,规格,
重量1=max(case when rn%8=1 then 净重 end),
重量2=max(case when rn%8=2 then 净重 end),
重量3=max(case when rn%8=3 then 净重 end),
重量4=max(case when rn%8=4 then 净重 end),
重量5=max(case when rn%8=5 then 净重 end),
重量6=max(case when rn%8=6 then 净重 end),
重量7=max(case when rn%8=7 then 净重 end),
重量8=max(case when rn%8=0 then 净重 end),
件数=COUNT(*),
合计重量=SUM(净重)
from cte
group by 型号,规格,(rn-1)/8
/*
型号 规格 重量1 重量2 重量3 重量4 重量5 重量6 重量7 重量8 件数 合计重量
---------- ---------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ----------- ---------------------------------------
bopp光膜 18*600 105.1 105.2 105.3 105.4 105.5 105.6 105.7 105.8 8 843.6
bopp光膜 18*600 105.4 105.6 105.1 105.2 NULL NULL NULL NULL 4 421.3
bopp光膜 18*610 105.1 105.1 105.2 NULL NULL NULL NULL NULL 3 315.4
bopp光膜 18*620 105.1 105.4 NULL NULL NULL NULL NULL NULL 2 210.5
bopp光膜 18*800 105.1 105.8 105.1 NULL NULL NULL NULL NULL 3 316.0
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
*/