decode函数技巧

   最近项目中需要统计项目/非项目工时,表中有工时字段,另外有两个是否项目和是否补录两个状态字段,需要分类型统计各种工时。想到用case when嵌套,但是没有成功,后来锋哥几乎2分钟用decode解决了问题,很强大!

   表结构如下:

-- Create table
create table T21_WORKTIME
(
  WORKDAY        VARCHAR2(10),
  USERNAME       VARCHAR2(12),
  WORKCOUNTS     NUMBER,
  ISPROJECT      VARCHAR2(1),
  STATUS         VARCHAR2(2),
  MYIDEA         VARCHAR2(512),
  DIRECTORIDEA   VARCHAR2(512),
  BRANCHIDEA     VARCHAR2(512),
  MANAGERIDEA    VARCHAR2(512),
  DIRECTORCOUNTS NUMBER,
  MANAGERCONTS   NUMBER,
  DEPTID         VARCHAR2(12),
  PROJECTID      VARCHAR2(12),
  ISADD          CHAR(1),
  WORKTIME_ID    VARCHAR2(22) not null,
  TYPE           VARCHAR2(2),
  D_CREATOR      VARCHAR2(32),
  D_CREATEDATE   CHAR(19),
  D_MODIFIER     VARCHAR2(32),
  D_MODIFYDATE   CHAR(19)
)
tablespace TSDAT02
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table T21_WORKTIME
  is '员工每天工时表';
-- Add comments to the columns 
comment on column T21_WORKTIME.WORKDAY
  is '日期(日)';
comment on column T21_WORKTIME.USERNAME
  is '用户编号';
comment on column T21_WORKTIME.WORKCOUNTS
  is '工时数量';
comment on column T21_WORKTIME.ISPROJECT
  is '是否项目工时';
comment on column T21_WORKTIME.STATUS
  is '审核状态';
comment on column T21_WORKTIME.MYIDEA
  is '审计员备注';
comment on column T21_WORKTIME.DIRECTORIDEA
  is '项目经理意见';
comment on column T21_WORKTIME.BRANCHIDEA
  is '分部经理意见';
comment on column T21_WORKTIME.MANAGERIDEA
  is '总经理意见';
comment on column T21_WORKTIME.DIRECTORCOUNTS
  is '项目经理调整数';
comment on column T21_WORKTIME.MANAGERCONTS
  is '总经理调整数';
comment on column T21_WORKTIME.DEPTID
  is '所属部门';
comment on column T21_WORKTIME.PROJECTID
  is '项目编码';
comment on column T21_WORKTIME.ISADD
  is '是否补录';
comment on column T21_WORKTIME.WORKTIME_ID
  is '工时编号';
comment on column T21_WORKTIME.TYPE
  is '非项目工时类别';
comment on column T21_WORKTIME.D_CREATOR
  is '创建人';
comment on column T21_WORKTIME.D_CREATEDATE
  is '创建时间';
comment on column T21_WORKTIME.D_MODIFIER
  is '修改人';
comment on column T21_WORKTIME.D_MODIFYDATE
  is '修改时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table T21_WORKTIME
  add constraint PK_T21_WORKTIME primary key (WORKTIME_ID)
  using index 
  tablespace TSDAT02
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

 

 

 

select s.username as username,
       substr(s.workday, 0, 7) as month,
       sum(decode(isadd || s.isproject, '01', nvl(s.workcounts, 0), 0)),
       sum(decode(isadd || s.isproject, '11', nvl(s.workcounts, 0), 0)),
       sum(decode(isadd || s.isproject, '00', nvl(s.workcounts, 0), 0)),
       sum(decode(isadd || s.isproject, '10', nvl(s.workcounts, 0), 0)),
       sum(s.workcounts),
       sum(s.directorcounts),
       sum(s.managerconts),
       sum(s.workcounts + s.directorcounts + s.managerconts)
  from T21_Worktime s
 where 1 = 1
   and s.username = '1776'
   and s.workday >= '2012-12-01'
   and s.workday <= '2012-12-31'
 group by s.username, substr(s.workday, 0, 7);

 

你可能感兴趣的:(decode)