sql case when

使用sql case   when 统计
SELECT XiaoQu ,count(1) errorTotal,sum(CASE WHEN ErrorState='new' THEN 1 ELSE 0 end) AS newError,
sum(CASE WHEN ErrorState='finish' THEN 1 ELSE 0 end) AS finish,sum(CASE WHEN ErrorState='discard' THEN 1 ELSE 0 end) AS discard
FROM [sam].[sam_errorList]

GROUP BY XiaoQu;


SELECT  t2.error_type, sum(CASE WHEN t1.ErrorState='new' THEN 1 ELSE 0 end) AS newError,
sum(CASE WHEN t1.ErrorState='finish' THEN 1 ELSE 0 end) AS finish,sum(CASE WHEN t1.ErrorState='discard' THEN 1 ELSE 0 end) AS discard
FROM [sam].[sam_errorList] t1,sam.sam_errorType t2
WHERE t1.ErrorID = t2.hid and XiaoQu = '鑫馨家园'
GROUP BY t2.error_type;

你可能感兴趣的:(sql)