值得一提的是,在公司实习的期间,有很多表格需要用到分组查询,然而并不会。还好大哥的耐心传授。
这是分组查询的SQL语句:
select to_char(t.begintime, 'DD') as YM, sum(nvl(t.power,0)) as sum_power
from cb_craneequipment t
where to_char(t.begintime, 'yyyy-MM') = '2017-02'
group by to_char(t.begintime, 'DD')
order by to_char(t.begintime, 'DD') asc
这其中需要注意的是:
1、 to_char(t.begintime, ‘DD’) 这个的作用就是把begintime的内容转换成为自己想要的格式,这个就是转换成“日”。同样,如果想要转成别的格式:
to_char(t.begintime, 'yyyy-MM') = '2017-02'
这个就是把时间转换成“年-月”。
2、group by to_char(t.begintime, ‘DD’),这个就是按照自己转换的“日”来进行分组。
3、order by to_char(t.begintime, ‘DD’) asc 就是让自己查到的内容以“日”的倒叙来输出,asc就是倒序输出。