大神帮忙看看这个sql题目要怎么做

我之前想的是修改表结构,增加m1,m2等4列,后来觉得题目要求是查询结果,所以应该是不能修改表结构。。。求大神帮助希望答案是验证过的哈。... 我之前想的是修改表结构,增加m1,m2等4列,后来觉得题目要求是查询结果,所以应该是不能修改表结构。。。
求大神帮助
希望答案是验证过的哈。
展开
 我来答
荼糜ZDW
2015-09-10 · TA获得超过118个赞
知道小有建树答主
回答量:126
采纳率:0%
帮助的人:143万
展开全部

试试这样,我验证过

select 
s.year,
sum(case when s.month='1' then  s.count else 0 end  )  as m1,
sum(case when s.month='2' then  s.count else 0 end  )  as m2,
sum(case when s.month='3' then  s.count else 0 end  )  as m3,
sum(case when s.month='4' then  s.count else 0 end  )  as m3
from table s
group by s.year
order by s.year

结果-------------------------------------

希望能帮到你。

helly84
2015-09-10 · TA获得超过329个赞
知道小有建树答主
回答量:737
采纳率:77%
帮助的人:319万
展开全部
select a.year
         ,case  when month = 1 then max(count) else "" end as m1
        ,case  when month = 2 then max(count) else "" end as m2
      ...
         ,case  when month = 12 then max(count) else "" end as m12
from (
select  year
          ,month
         ,count
         ,row_number( ) over (partition by year order by month) as order_no
 from table ) a
 group by a.year
order by a.year

可以对应有重复数据的情况。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式