日期:2014-05-17 浏览次数:20406 次
--id,name,sex,班组, 登记时间
DECLARE @month1 CHAR(7), @month2 CHAR(7)
SELECT @month1 = '2013-03', @month2 = '2013-04'
SELECT
班组 = COALESCE(a.班组, b.班组),
[数量1] = ISNULL(a.cnt, 0),
[数量2] = ISNULL(b.cnt, 0),
差值 = ISNULL(a.cnt, 0) - ISNULL(b.cnt, 0)
FROM
(
SELECT 班组, cnt=COUNT(1)
FROM TB
WHERE CONVERT(CHAR(7),登记时间,120) = @month1
GROUP BY 班组
) a
FULL JOIN
(
SELECT 班组, cnt=COUNT(1)
FROM TB
WHERE CONVERT(CHAR(7),登记时间,120) = @month2
GROUP BY 班组
) b
ON a.班组 = b.班组