SQL面试题(十二)

问题:

表如下: 销售表:

   NO ,  NO2,  数量   日期

  1234 567890  33.5  2004-12-21

  1234 598701  44.8  2004-11-21

  1234 598701  45.2  2004-10-01

  1234 567890  66.5  2004-9-21

  3456 789065  22.5   2004-10-01

  3456 789065  77.5   2004-10-27

  3456 678901  48.5   2004-12-21

 

按月统计销售表中货物的销售量数

查询结果如下:

  No No2    九月,  十月,十一月,十二月

1234567890 66.5 ,  0    33.5

1234598701  0   , 45.2, 44.8, 0

3456789065, ,  100  ,  0

3456678901, 0 ,    0    0    48.5

建表:

create table table_a (No int, No2 int,num double,itime date);
insert into table_a values 
(1234,567890,33.5,'2004-12-21'),
(1234,598701,44.8,'2004-11-21'),
(1234,598701,45.2,'2004-10-01'),
(1234,567890,66.5,'2004-9-21'),
(3456,789065,22.5,'2004-10-01'),
(3456,789065,77.5,'2004-10-27'),
(3456,678901,48.5,'2004-12-21');

 

Sql:

select NO,NO2,
     sum(case  when itime like '2004-%9%' then num else 0 end) as 9M,
     sum(case  when itime like '2004-10%' then num else 0 end) as 10M,
     sum(case  when itime like '2004-11%' then num else 0 end) as 11M,
    sum(case  when itime like '2004-12%' then num else 0 end) as 12M
    from table_a group by no,no2 order by no,no2 ;

 

结果:

+------+--------+------+------+------+------+

| NO   | NO2    | 9M   | 10M  | 11M  | 12M  |

+------+--------+------+------+------+------+

| 1234 | 567890 | 66.5 |    0 |    0 | 33.5 |

| 1234 | 598701 |    0 | 45.2 | 44.8 |    0 |

| 3456 | 678901 |    0 |    0 |    0 | 48.5 |

| 3456 | 789065 |    0 |  100 |    0 |    0 |

+------+--------+------+------+------+------+

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值