日期:2014-05-17 浏览次数:20599 次
DECLARE @t TABLE(d INT,p VARCHAR(10));
INSERT INTO @T SELECT 1,'感冒药'
UNION ALL SELECT 1,'消炎药'
UNION ALL SELECT 1,'消炎药'
UNION ALL SELECT 2,'感冒药'
UNION ALL SELECT 2,'消炎药'
SELECT d,SUM(CASE WHEN p='感冒药' THEN 1 END ) AS 感冒药,COUNT(*) AS 药,SUM(CASE WHEN p='感冒药' THEN 1 END )*100.0/COUNT(*) 感冒药比例 FROM @T GROUP BY d
/*
(5 行受影响)
d 感冒药 药 感冒药比例
----------- ----------- ----------- ---------------------------------------
1 1 3 33.333333333333
2 1 2 50.000000000000
*/