日期:2014-05-17 浏览次数:20513 次
----奖品发放记录表-----
create table share_prizes (
id INT identity(1,1) PRIMARY KEY, --主键
giftId int, --对应奖品表ID
name varchar(20), --奖品名称
deliveryTime smalldatetime --奖品发放时间
)
insert into table share_prizes (gifID,name,deliveryTime) values(1,'ipad3','2012-08-12');
insert into table share_prizes (gifID,name,deliveryTime) values(2,'苹果笔记本','2012-09-12');
insert into table share_prizes (gifID,name,deliveryTime) values(3,'iphone5','2012-10-12');
insert into table share_prizes (gifID,name,deliveryTime) values(4,'平板电视','2012-11-12');
insert into table share_prizes (gifID,name,deliveryTime) values(5,'ipad3','2012-11-12');
SELECT
CONVERT(VARCHAR(7),deliverytime,120) AS '月份',
SUM(CASE WHEN NAME = 'ipad3' THEN 1 ELSE 0 END) AS 'ipad3',
SUM(CASE WHEN NAME = '苹果笔记本' THEN 1 ELSE 0 END) AS '苹果笔记本',
SUM(CASE WHEN NAME = 'iphone5' THEN 1 ELSE 0 END) AS 'iphone5',
SUM(CASE WHEN NAME = '平板电视' THEN 1 ELSE 0 END) AS '平板电视'
FROM share_prizes
GROUP BY CONVERT(VARCHAR(7),deliverytime,120)
/*
2012-08 1 0 0 0
2012-09 0 1 0 0
2012-10 0 0 1 0
2012-11 1 0 0 1*/