日期:2014-05-17 浏览次数:20574 次
if OBJECT_ID('baseInfo') is not null
drop table baseInfo
go
create table baseInfo
(
code int identity(1,1),
name varchar(20)
)
if OBJECT_ID('xsjl') is not null
drop table baseInfo
go
create table xsjl
(
name varchar(20),
mdate date,
sales decimal(12,2)
)
insert into baseInfo(name)
select 'comp1' union all
select 'comp2'
insert into xsjl
select 'comp1','2009-1-5',10 union all
select 'comp1','2009-5-5',10 union all
select 'comp1','2010-1-5',30 union all
select 'comp1','2010-5-5',30 union all
select 'comp1','2011-1-5',50 union all
select 'comp1','2011-5-5',50 union all
select 'comp2','2009-1-5',50 union all
select 'comp2','2011-5-5',50
go
select
t.name,
sum(case YEAR(t1.mdate) when 2009 then sales else 0 end)'企业09年度销售额',
sum(case YEAR(t1.mdate) when 2010 then sales else 0 end) '企业09年度销售额',
sum(case YEAR(t1.mdate) when 2011 then sales else 0 end) '企业09年度销售额'
from
baseInfo t
inner join xsjl t1 on t.name=t1.name
group by t.name