SQL 经典例题条件查询

3106人阅读 2021/6/9 20:11 总访问:651561 评论:0 收藏:1 手机
分类: SQL

数据表

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






评价
脚踏实地,一步一个脚印
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术