日期:2014-05-18 浏览次数:20443 次
select 销售小组='第一组', 产品1销售金额=(select isnull(sum(产品1金额),0) from report where 销售小组='第1组'), 产品2销售金额=(select isnull(sum(产品2金额),0) from report where 销售小组='第1组'), 合同1签约数=(select count(*) from report where 合同1=‘有效’), 得分=(select isnull(sum(产品1金额),0) from report where 销售小组='第1组')/10000+ (select isnull(sum(产品2金额),0) from report where 销售小组='第1组')/10000 +(select count(*) from report where 合同1=‘有效’) union ......(小组2的记录,语句同小组1)
select 销售小组,sum(产品1销售金额) as 产品1金额,sum(产品2金额) as 产品2金额 from report group by 销售小组
select 销售小组,count(*) as 合同1签约数 from report where 合同1='有效' group by 销售小组
go if OBJECT_ID('report')is not null drop table report go create table report( 销售人员 varchar(10), 销售小组 varchar(8), 产品1销售金额 money, 产品1销售日期 date, 产品2销售金额 money, 产品2销售日期 date, 合同1 varchar(4), 合同1签约日期 date ) go insert report select '张三','第一组',10,'2012-04-01',5,'2012-04-02',null,null union all select '李四','第二组',null,null,null,null,'有效','2012-04-03' union all select '王五','第三组',null,null,5,'2012-04-10','无效','2012-04-2' select * from report select 销售小组,sum(isnull(产品1销售金额,0)) as 产品1金额, sum(isnull(产品2销售金额,0)) as 产品2金额, sum(case when 合同1='有效' then 1 else 0 end) as 合同1数量, sum(isnull(产品1销售金额,0)+isnull(产品2销售金额,0)+ (case when 合同1='有效' then 1 else 0 end))得分 from report where 产品1销售日期='2012-04-01' or 产品2销售日期='2012-04-01' or 合同1签约日期='2012-04-01' group by 销售小组 /* 销售小组 产品1金额 产品2金额 合同1数量 得分 第一组 10.00 5.00 0 15.00 */ --是这个意思吗?