select to_char((trunc(sysdate)+trunc((日期-trunc(sysdate))*24)/24),'hh24') ||'-'||to_char((trunc(sysdate)+trunc((日期-trunc(sysdate))*24+1)/24),'hh24') 時間段, avg(統(tǒng)計字段平均值),sum(統(tǒng)計字段和),count(*) "字段條數(shù)"
進賢網(wǎng)站建設公司成都創(chuàng)新互聯(lián),進賢網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經驗。已為進賢超過千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿營銷網(wǎng)站建設要多少錢,請找那個售后服務好的進賢做網(wǎng)站的公司定做!
from 表名
where trunc(日期)=to_date(day,'yyyymmdd')
group by to_char((trunc(sysdate)+trunc((日期-trunc(sysdate))*24)/24),'hh24')||'-'|| to_char((trunc(sysdate)+trunc((日期-trunc(sysdate))*24+1)/24),'hh24')
order by 1;
兩種方式 :
1、可以直接用SQL 里的函數(shù) sum() ;
如:"select sum(字段) as num from tab "; //sum 取該字段所有列的總和
2、用PHP函數(shù) sum($array) ; //取數(shù)組中值的總和
當然 字段必須是int型的
時間段統(tǒng)計,可以采用 hour(subscribe_time) 取出小時然后分層。思路:
select
uid
,CASE WHEN HOUR(subscribe_time) BETWEEN 0 AND 1 THEN '00:00:00'
WHEN HOUR(subscribe_time) BETWEEN 2 AND 3 THEN '02:00:00'
...
ELSE '23:00:00' END -- 生成時間分層部分,insert前外層sql加上日期后作為唯一的時間值
,COUNT(*)
FROM yht_fans WHERE subscribe_time=UNIX_TIMESTAMP(CURDATE()) and subscribe_time=UNIX_TIMESTAMP(CURDATE())+86400
GROUP BY 1,2
時間段統(tǒng)計,可以采用 hour(subscribe_time) 取出小時然后分層。思路:
select
uid
,CASE WHEN HOUR(subscribe_time) BETWEEN 0 AND 1 THEN '00:00:00'
WHEN HOUR(subscribe_time) BETWEEN 2 AND 3 THEN '02:00:00'
...
ELSE '23:00:00' END -- 生成時間分層部分,insert前外層sql加上日期后作為唯一的時間值
,COUNT(*)
FROM yht_fans WHERE subscribe_time=UNIX_TIMESTAMP(CURDATE()) and subscribe_time=UNIX_TIMESTAMP(CURDATE())+86400
GROUP BY 1,2
select DATE_FORMAT(來電時間,'%Y-%m') as 月, 問題類別 , count(1) as 數(shù)量 from
( select 來電時間, 問題類別 from mytable where 問題類別 = '農村用水投訴' ) tmptable
group by DATE_FORMAT(來電時間,'%Y-%m') ,問題類別 ORDER BY 月 ASC;