深圳市奔凯安全技术股份有限公司面试题之一:关于数据库查询,根据年份查询每个季度的销售额

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hch15112345824/article/details/74157526

题目:根据年份查询每个季度的销售额,表的结构大概如下:(year是年,month是季度,sale是销售量,表 名 是 sales)

select * from sales;

他要求写sql语句,显示如下图:(一季度就是month=1,二季度就是month=2......)


面试的时候不会写,面试以后回家自己写了一下,sql语句如下:

SELECT year,
sum(case when month='1' then sale end) as '一季度',
sum(case when month='2' then sale end) as '二季度',
sum(case when month='3' then sale end) as '三季度',
sum(case when month='4' then sale end) as '四季度'
 from sales GROUP BY `year`



猜你喜欢

转载自blog.csdn.net/hch15112345824/article/details/74157526