create table aa(
typeId int,
name varchar(20))
create table bb(
id int ,
name varchar(20),
typeId int ,
num int ,
m int
)
insert into aa values(1,'自行车'),(2,'摩托车'),(3,'小汽车'),(4,'其它')
insert into bb values
(1,'li',1,1,500),
(2,'li',2,2,5000),
(3,'li',3,1,50000),
(4,'wanger',1,1,450),
(5,'wanger',2,1,5600)
select bb.name,
sum(case when aa.name ='自行车' then bb.num
else 0
end ) as [自行车num],
sum(case when aa.name ='自行车' then bb.m
else 0
end ) as [自行车m],
sum(case when aa.name ='摩托车' then bb.num
else 0
end ) as [摩托车num],
sum(case when aa.name ='摩托车' then bb.m
else 0
end ) as [摩托车m],
sum(case when aa.name ='小汽车' then bb.num
else 0
end ) as [小汽车num],
sum(case when aa.name ='小汽车' then bb.m
else 0
end ) as [小汽车m],
sum(case when aa.name ='其它' then bb.num
else 0
end ) as [其它num],
sum(case when aa.name ='其它' then bb.m
else 0
end ) as [其它m] ,
sum(bb.num) as [Allnum],
sum(bb.m ) as [Allm]
from aa right join bb on aa.typeId=bb.typeId
group by bb.name
truncate table aa
drop table aa
truncate table bb
drop table bb
go
mysql 脚本的方法,请对照这个 sql server脚本去修改,如有疑问,及时沟通
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)