日期:2014-05-17 浏览次数:20565 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (备件 nvarchar(2),百分比 numeric(4,3))
insert into [TB]
select 'A',0.270 union all
select 'B',0.250 union all
select 'C',0.200 union all
select 'D',0.180 union all
select 'E',0.100
select * from [TB]
SELECT B.备件 ,
B.百分比 ,
CASE WHEN SUM(A.百分比) BETWEEN 0 AND 0.5 THEN 'A'
ELSE CASE WHEN SUM(A.百分比) BETWEEN 0.5 AND 0.9 THEN 'B'
ELSE 'C'
END
END AS ABC分类
FROM dbo.TB A
INNER JOIN TB B ON A.备件 <= B.备件
GROUP BY B.备件 ,
B.百分比
ORDER BY B.备件
/*
备件 百分比 ABC分类
A 0.270 A
B 0.250 B
C 0.200 B
D 0.180 B
E 0.100 C*/
SELECT
备件,
百分比,
(SELECT
CASE WHEN 百分比 < 0.5 THEN 'A'
ELSE
CASE WHEN 百分比>=0.5 AND 百分比 < 0.9 THEN 'B'
ELSE 'C'
END
END
)
FROM tb