Session

SQL 经典例题条件查询

电脑版发表于:2021/6/9 20:11

数据表

create table temp (

year int,

month int,

amout float

)

insert temp values(1991,1,1.1)

insert temp values(1991,2,1.2)

insert temp values(1991,3,1.3)

insert temp values(1991,4,1.4)

insert temp values(1992,1,2.1)

insert temp values(1992,2,2.2)

insert temp values(1992,3,2.3)

insert temp values(1992,4,2.4)



实现根据查询达到以下效果

  select YEAR
  ,SUM(case when MONTH = 1 then amout else 0 end) as m1
  ,SUM(case when MONTH = 2 then amout else 0 end) as m2
  ,SUM(case when MONTH = 3 then amout else 0 end) as m3
  ,SUM(case when MONTH = 4 then amout else 0 end) as m4
  from temp
  group by year






关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢