oracle-需要学习的sql语句-1

--每月拍行
select e.danWei, e.faBu, e2.qianShou, (e.faBu - e2.qianShou) zongFen
     from (select d.d_code code, d.d_name danWei, count(m.m_id) faBu
           from msg_dept d, msg_message m
          where d.d_id = m.m_deptid(+)
            and to_char(m_datetime(+), 'yyyy-mm') =
                to_char(sysdate, 'yyyy-mm')
          group by d.d_name,d.d_code) e,
        (select d.danWei, s.qianShou
           from (select s_userid as userid,
                        sum(decode(s_signindate, null, 1, 0)) as qianShou
                   from msg_sign_in
                  group by s_userid) s,
                (select d.d_name as danWei, u.u_id as userid
                   from msg_dept d, msg_user u
                  where d.d_id = u.u_deptid) d
          where s.userid = d.userid) e2
  where e.danWei = e2.danWei order by e.code;



--判断
select decode(T.gx_dept_name,'香蜜湖公交所',substr(T.gx_dept_name, 0, 3),substr(T.gx_dept_name, 0, 2)) as dept_name,	--当T.gx_dept_name的值为“香蜜湖公交所”时截取前三个字符
       decode(T.num, null, 0, T.num) as num,				--当T.num为空时值为0当不为空时取T.num的值
       decode(TT.num2, null, 0, TT.num2) as num2
  from (select gx_dept_name, count(d_id) as num
          from msg_data_source
         where jqxz = '刑事警情'
           and fa_date > sysdate - 190
           and jqlb = '扒窃'
         group by gx_dept_name) T,
       (select gx_dept_name, count(d_id) as num2
          from msg_data_source
         where jqxz = '刑事警情'
           and create_date > sysdate - 1
           and jqlb = '扒窃'
         group by gx_dept_name) TT
 where T.gx_dept_name = TT.gx_dept_name(+);


--根据sql语句生成数据唯一编号(规则:业务名称 + 年 + 月 + 日 + 四位递增数)
表结构:
create table SLBH_LIST
(
  YWBH VARCHAR2(50),
  SLBH VARCHAR2(50),
  CJSJ DATE default sysdate not null,
  XGSJ DATE,
  SM   VARCHAR2(200)
)
tablespace HLDB_DATA
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
comment on table SLBH_LIST
  is '受理编号记录';
comment on column SLBH_LIST.SM
  is '说明';


sql语句:
select case
         when to_number(substr(t.slid, 13, 8)) <
              to_number(to_char(sysdate, 'yyyymmdd')) then
          'HLW-RK-HKBG-' || to_char(sysdate, 'yyyymmdd') || '0001'
         when to_number(substr(t.slid, 13, 8)) =
              to_number(to_char(sysdate, 'yyyymmdd')) then
          substr(t.slid,
                 1,
                 length(t.slid) -
                 length(to_number(substr(t.slid, 21, 4)) + 1)) ||
          to_char(to_number(substr(t.slid, 21, 4)) + 1)
       end
  from (select slbh slid from slbh_list where ywbh = 'HLW-RK-HKBG') t

猜你喜欢

转载自x125858805.iteye.com/blog/1600424