MYSQL 统计语句

MYSQL 统计语句,第1张

select sum(ordercount) from orders where teamId = 2 这样就行了~!!~如果Teamid 是字符类型的 就的用select sum(ordercount) from orders where teamId = '2'

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脚本去修改,如有疑问,及时沟通


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6119173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-15
下一篇 2023-03-15

发表评论

登录后才能评论

评论列表(0条)

保存