zoukankan      html  css  js  c++  java
  • oracle按月、日、时分组查询数据,为空的数据补零

    ------月

    select nvl(t1.tvalue, 0) "data1", t2.datevalue "name"
      from (select sum(t.TSAI03) tvalue, TO_CHAR(t.TSAI01, 'yyyy-mm') timevalue
              from TSA009 t
             where TO_CHAR(t.TSAI01, 'YYYY-MM-DD') like '2012%'
               and t.unit_code like '411500A0050000'
             group by TO_CHAR(t.TSAI01, 'yyyy-mm')) t1,
           (select '2012-' || lpad(level, 2, 0) datevalue
              from dual
            connect by level < 13) t2
     where t1.timevalue(+) = t2.datevalue
     order by t2.datevalue
    

    -----日

    select nvl(t1.tvalue, 0) "data1", t2.datevalue "name"
      from (select sum(t.TSAI03) tvalue,
                   TO_CHAR(t.TSAI01, 'yyyy-mm-dd') timevalue
              from TSA009 t
             where TO_CHAR(t.TSAI01, 'YYYY-MM-DD') like '2012-04%'
               and t.unit_code like '411500A0050000'
             group by TO_CHAR(t.TSAI01, 'yyyy-mm-dd')) t1,
           (select '2012-04-' || lpad(level, 2, 0) datevalue
              from dual
            connect by level < (select to_number(substr(last_day(to_date('2012-04-10',
                                                                         'yyyy-mm-dd')),
                                                        0,
                                                        2))
                                  from dual) + 1) t2
     where t1.timevalue(+) = t2.datevalue
     order by t2.datevalue
    

    ----时

    select nvl(t1.tvalue, 0) "data1", t2.datevalue "name"
      from (select sum(t.TSAJ03) tvalue,
                   TO_CHAR(t.TSAJ01, 'yyyy-mm-dd hh24') timevalue
              from TSA010 t
             where TO_CHAR(t.TSAJ01, 'YYYY-MM-DD') like '2012-04-10%'
               and t.unit_code like '411500A0050000'
             group by TO_CHAR(t.TSAJ01, 'yyyy-mm-dd hh24')) t1,
           (select '2012-04-10 ' || lpad(level, 2, 0) datevalue
              from dual
            connect by level < 25) t2
     where t1.timevalue(+) = t2.datevalue
     order by t2.datevalue
    
  • 相关阅读:
    安卓学习第12课——SimpleAdapter
    用栈结构实现多项式计算器
    用B-树实现虚拟图书管理系统
    HDU4791【杂】
    HDU4801【DFS】
    萌新学习图的强连通(Tarjan算法)笔记
    Lightoj 1021【状压DP(未搞)】
    Lightoj 1008【规律】
    CodeForces Canada Cup 2016【A,B,C,D】
    51nod 1068【简单博弈】
  • 原文地址:https://www.cnblogs.com/GenghisKhan/p/2584571.html
Copyright © 2011-2022 走看看