oracle 按天數統計資料____oracle

來源:互聯網
上載者:User
當使用group by crateTime時,但是某天沒有資料時,就出現某天沒有資料了.

所以使用以下sql,能把每天的都統計出來.

        select 
            to_date('2015-06-01', 'yyyy-mm-dd')+level -1 as 時間         from dual  connect by to_date('2015-06-01', 'yyyy-mm-dd') + level -1 <= to_date('2015-06-04', 'yyyy-mm-dd');              select             (to_date('2015-06-01','yyyy-mm-dd')+level - 1) as 時間         from dual connect by level <= to_date('2015-06-04','yyyy-mm-dd') - to_date('2015-06-01','yyyy-mm-dd') + 1;

               查詢結果如下:

時間
2015-6-1
2015-6-2
2015-6-3
2015-6-4

然後再使用left on 連結去查詢統計資料:如

select c_time,nvl(totalUser,0) as total_user from (        select   (to_date('2015-06-01','yyyy-mm-dd')+level - 1) as c_time  from dual connect by level <= to_date('2015-06-11','yyyy-mm-dd') - to_date('2015-06-01','yyyy-mm-dd') + 1        ) a left join (          select trunc(u.create_time) as create_time,count(*) as totalUser from t_user u group by trunc(u.create_time)        ) b on a.c_time = b.create_time order by c_time 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.