日期:2014-05-17 浏览次数:20476 次
-->try
select Model,
status,
LineA_PASS=(case when Model='A' and status='PASS' then LineA else 0 end),
LineA_FAIL=(case when Model='A' and status='FAIL' then LineA else 0 end),
LineB_PASS=(case when Model='B' and status='PASS' then LineB else 0 end),
LineB_FAIL=(case when Model='B' and status='FAIL' then LineB else 0 end),
LineC_PASS=(case when Model='C' and status='PASS' then LineC else 0 end),
LineC_FAIL=(case when Model='C' and status='FAIL' then LineC else 0 end)
from
(
select Model,status,sum(LineA) LineA,sum(LineB) LineB,sum(LineC) LineC
from 表
group by Model,status
)t
select Model,
status,
LineA_PASS=sum(case when Model='A' and status='PASS' then LineA else 0 end),
LineA_FAIL=sum(case when Model='A' and status='FAIL' then LineA else 0 end),
LineB_PASS=sum(case when Model='B' and status='PASS' then LineB else 0 end),
LineB_FAIL=sum(case when Model='B' and status='FAIL' then LineB else 0 end),
LineC_PASS=sum(case when Model='C' and status='PASS' then LineC else 0 end),
LineC_FAIL=sum(case when Model='C' and status='FAIL' then LineC else 0 end)
from
tb
USE tempdb
GO
CREATE TABLE test
(
Model CHAR(2),
status CHAR(4),
LineA INT ,
LineB INT,
lineC INT
)
INSERT INTO test
SELECT 'A' ,'FAIL', 9, 0 ,0
UNION ALL
SELECT 'A', 'PASS', 10, 0 ,0
UNION ALL
SELECT 'A' ,'PASS', 12 ,0 ,0
UNION ALL
SELECT 'B', 'FAIL', 0, 9 ,0
UNION ALL
SELECT 'B', 'FAIL' ,0 ,2 ,0
UNION ALL
SELECT 'B', 'PASS', 0, 5 ,0
UNION ALL
SELECT 'C', 'FAIL', 0 ,9 ,0
UNION ALL
SELECT 'C', 'FAIL', 0, 7 ,0
UNION ALL
SELECT 'C', 'PASS', 0 ,6 ,0
SELECT Model,&nbs