|
发表于 2020-5-30 20:45:02
|
显示全部楼层
create table ta(日期 varchar(5), 型号 varchar(2), 销售量 int)
insert ta
select '12.1', 'A', 2
union all select '12.1', 'B', 1
union all select '12.1', 'C', 1
union all select '12.1', 'E', 3
union all select '12.1', 'F', 1
union all select '12.1', 'H', 2
union all select '12.1', 'I', 1
union all select '12.2', 'A', 1
union all select '12.2', 'C', 1
union all select '12.2', 'D', 1
union all select '12.2', 'E', 4
union all select '12.2', 'H', 2
为了简化语句用变量:
declare @sql varchar(2000)
set @sql='(select 型号,销售号=sum(销售量),
百分比=str(convert(numeric(5,2),sum(销售量))/(select sum(销售量) from ta),5,2)
from ta group by 型号)'
set @sql='select *,(select sum(cast (百分比 as numeric(5,3))) from '+@sql
+' as tb where 百分比!>b.百分比)as 递减'
+' from '+@sql+' as b '
set @sql='select 型号,销售号,百分比 from ('+@sql+') as ta where 递减>.5'--占的比列
--print @sql
exec (@sql)
型号 销售号 百分比
---- ----------- -----
E 7 0.35
H 4 0.20
declare @sql varchar(2000)
set @sql='(select 型号,销售号=sum(销售量),
百分比=str(convert(numeric(5,2),sum(销售量))/(select sum(销售量) from ta),5,2)
from ta group by 型号)'
set @sql='select *,(select sum(cast (百分比 as numeric(5,3))) from '+@sql
+' as tb where 百分比!>b.百分比)as 递减'
+' from '+@sql+' as b '
set @sql='select 型号,销售号,百分比 from ('+@sql+') as ta where 递减>.2'--大于占20%=占80%
--print @sql
exec (@sql)
型号 销售号 百分比
---- ----------- -----
A 3 0.15
C 2 0.10
E 7 0.35
H 4 0.20
|
|