case when 写在where条件中

select datas.id,
       datas.name,
       datas.memo,
       to_char(datas.create_date, 'yyyy-mm-dd hh24:mi:ss'),
       to_char(datas.create_date, 'yyyy-mm-dd hh24:mi:ss')
  from t_stl_s_backlog datas,
       (select t.id as id,
               t.stage as stage,
               (case
                 when t.type in ('37', '38') then --  客户信息
                  'CUSTOMER'
                 when t.type in ('39', '40') then -- 项目信息
                  'PROJECT'
                 when t.type in ('41', '42') then -- 租赁物信
                  'LEASEHOLD'
                 when t.type in ('43', '44') then -- 商业机会信息
                  'OPPORTUNITY'
                 when t.type in ('45', '46') then -- 交易信息
                  'TRADEINFO'
                 when t.type in ('47', '48') then -- 交易机会(债券)
                  'TRADINGOPPORTUNITY'
                 when t.type in ('53', '54') then -- 交易机会(贷款)
                  'TRADINGOPPORTUNITYLOAN'
                 when t.type in ('49', '50') then -- 租期检查信息  
                  'LEASECHECK'
                 when t.type in ('51', '52') then -- 质量分类信息
                  'QUALITYCLASSIFY'
                 else
                  'other'
               end) as typeMark
          from t_stl_s_backlog t
         inner join t_stl_s_organization organizati1_
            on t.fk_organization_id = organizati1_.id
         where t.type in ('37',
                          '38',
                          '39',
                          '40',
                          '41',
                          '42',
                          '47',
                          '48',
                          '43',
                          '44',
                          '45',
                          '46',
                          '49',
                          '50',
                          '51',
                          '52',
                          '53',
                          '54')
           and organizati1_.id in
               ('40280c1250ac38680150ac5845f20004',
                '40280c1250ac38680150ac5a74ff0007',
                '40280c1250ac38680150ac5afed20008',
                '40280c66508e2f7e01508e4133ce0000',
                '40280c1250ac38680150ac58235c0003',
                '1',
                '40280c96533a921c01533a9b2be30000',
                '40280c96533a921c01533a9c40fb0001',
                '4028d08154c86e630154c8c5804e0001')
           and to_char(t.end_date, 'yyyy-mm-dd hh24:mi:ss') >=
               '2016-08-24 00:00:00') data1,
       (select nvl(max(a.analyze_customer), 0) as analyze_customer, --客户分析阶段
               nvl(max(a.censor_customer), 0) as censor_customer, --客户审查阶段
               nvl(max(a.compliance_review_project), 0) as compliance_review_project, -- 项目合规审查阶段
               nvl(max(a.dispose_leasehold), 0) as dispose_leasehold, -- 租赁物处置阶段
               nvl(max(a.due_diligence_project), 0) as due_diligence_project, --项目尽职调查阶段
               nvl(max(a.examine_project), 0) as examine_project, -- 项目项目审批阶段
               nvl(max(a.factor_contract), 0),
               nvl(max(a.finish_contract), 0),
               nvl(max(a.initiation_opportunity), 0) as initiation_opportunity, -- 商业机会项目立项阶段
               nvl(max(a.insure_leasehold), 0) as insure_leasehold, -- 租赁物保险阶段
               nvl(max(a.intervene_opportunity), 0) as intervene_opportunity, -- 商业机会前期介入阶段
               nvl(max(a.investigate_customer), 0) as investigate_customer, -- 客户调查阶段
               nvl(max(a.manage_leasehold), 0) as manage_leasehold, -- 租赁物租期管理阶段
               nvl(max(a.prepare_contract), 0),
               nvl(max(a.pricing_leasehold), 0) as pricing_leasehold, -- 租赁物认定及定价阶段
               nvl(max(a.register_leasehold), 0) as register_leasehold, -- 租赁物交接及登记阶段
               nvl(max(a.sign_contract), 0),
               nvl(max(a.sign_project), 0) as sign_project, -- 项目 签约付款阶段
               nvl(max(a.start_contract), 0),
               nvl(max(a.contact_customer), 0) as contact_customer, -- 客户联络阶段
               nvl(max(a.project_start), 0),
               nvl(max(a.review_project), 0) as review_project, -- 项目评审审查阶段
               nvl(max(a.cancel_guarantee_contract), 0),
               nvl(max(a.prepare_guarantee_contract), 0),
               nvl(max(a.sign_guarantee_contract), 0),
               nvl(max(a.commu_trading_opportunity), 0) as commu_trading_opportunity, -- 交易机会沟通阶段
               nvl(max(a.init_trading_opportunity), 0) as init_trading_opportunity, -- 交易机会立项阶段
               nvl(max(a.finish_financing_contract), 0),
               nvl(max(a.prepare_financing_contract), 0),
               nvl(max(a.sign_financing_contract), 0),
               nvl(max(a.credit_apply), 0) as credit_apply, -- 交易授信申请阶段
               nvl(max(a.credit_approve), 0) as credit_approve, -- 交易授信获批阶段
               nvl(max(a.credit_end), 0) as credit_end, -- 交易授信到期阶段
               nvl(max(a.credit_start), 0) as credit_start, -- 交易授信使用阶段
               nvl(max(a.analysis_lease_check), 0) as analysis_lease_check, --  租期检测的分析阶段
               nvl(max(a.analysis_quality_classify), 0) as analysis_quality_classify, -- 质量分类初分阶段
               nvl(max(a.approve_quality_classify), 0) as approve_quality_classify, -- 质量分类中分阶段
               nvl(max(a.execute_lease_check), 0) as execute_lease_check, -- 租期检查执行阶段
               nvl(max(a.plan_lease_check), 0) as plan_lease_check, -- 租期检测的计划阶段
               nvl(max(a.plan_quality_classify), 0) as plan_quality_classify -- 质量分类计划阶段
          from (select qx.*, zjb.user_id as fk_user_id
                  from T_STL_S_POSITION_PRIVILEGE qx,
                       T_STL_S_POSITION           gw,
                       T_STL_S_USER_POSITION      zjb
                 where zjb.user_id = '4028ef8156a07f530156a08b3cb3010b'
                   and zjb.position_id = gw.id
                   and gw.fk_privilege_id = qx.id) a
         group by a.fk_user_id) data2

 where datas.id = data1.id
      -- 建立阶段与岗位权限的映射关系 
   and (case
       -- 客户阶段权限
         when data1.typeMark = 'CUSTOMER' and data1.stage = '1' then -- 开始阶段对应的是客户联络阶段
          data2.contact_customer
         when data1.typeMark = 'CUSTOMER' and data1.stage = '2' then -- 联络阶段对应的是客户联络阶段
          data2.contact_customer
         when data1.typeMark = 'CUSTOMER' and data1.stage = '3' then -- 调查阶段对应的是客户调查阶段
          data2.investigate_customer
         when data1.typeMark = 'CUSTOMER' and data1.stage = '4' then -- 审查阶段对应的是客户审查阶段
          data2.censor_customer
         when data1.typeMark = 'CUSTOMER' and data1.stage = '5' then -- 分析阶段对应的是客户分析阶段
          data2.analyze_customer
         when data1.typeMark = 'CUSTOMER' and data1.stage = '6' then -- 结束阶段对应的是客户分析阶段
          data2.analyze_customer
       -- 项目
         when data1.typeMark = 'PROJECT' and data1.stage = '1' then -- 开始阶段对应的是项目开始阶段
          data2.due_diligence_project
         when data1.typeMark = 'PROJECT' and data1.stage = '2' then -- 尽职调查阶段对应的是项目尽职调查阶段
          data2.due_diligence_project
         when data1.typeMark = 'PROJECT' and data1.stage = '3' then -- 合规审查阶段对应的是项目合规审查阶段
          data2.compliance_review_project
         when data1.typeMark = 'PROJECT' and data1.stage = '4' then -- 评审审查阶段对应的是项目评审审查阶段
          data2.review_project
         when data1.typeMark = 'PROJECT' and data1.stage = '5' then -- 项目审批阶段对应的是项目项目审批阶段
          data2.examine_project
         when data1.typeMark = 'PROJECT' and data1.stage = '6' then -- 签约付款阶段对应的是项目签约付款阶段
          data2.sign_project
         when data1.typeMark = 'PROJECT' and data1.stage = '7' then -- 结束阶段对应的是项目签约付款阶段
          data2.sign_project
       -- 租赁物
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '1' then -- 开始阶段对应的是租赁物认定及定价阶段
          data2.pricing_leasehold
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '2' then -- 认定及定价阶段对应的是租赁物认定及定价阶段
          data2.pricing_leasehold
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '3' then -- 交接及登记价阶段对应的是租赁物交接及登记阶段
          data2.register_leasehold
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '4' then -- 保险价阶段对应的是租赁物保险价阶段
          data2.insure_leasehold
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '5' then -- 租期管理阶段对应的是租赁物租期管理阶段
          data2.manage_leasehold
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '6' then -- 处置阶段对应的是租赁物处置阶段
          data2.dispose_leasehold
         when data1.typeMark = 'LEASEHOLD' and data1.stage = '7' then -- 结束阶段对应的是租赁物处置阶段
          data2.dispose_leasehold
       -- 商业机会  
         when data1.typeMark = 'OPPORTUNITY' and data1.stage = '1' then -- 开始阶段对应的是商业机会前期介入阶段
          data2.intervene_opportunity
         when data1.typeMark = 'OPPORTUNITY' and data1.stage = '2' then -- 前期介入阶段对应的是商业机会前期介入阶段
          data2.intervene_opportunity
         when data1.typeMark = 'OPPORTUNITY' and data1.stage = '3' then -- 项目立项阶段对应的是商业机会项目立项阶段
          data2.initiation_opportunity
         when data1.typeMark = 'OPPORTUNITY' and data1.stage = '4' then -- 结束阶段对应的是商业机会项目立项阶段
          data2.initiation_opportunity
       -- 交易信息    
         when data1.typeMark = 'TRADEINFO' and data1.stage = '1' then -- 开始阶段对应的是交易授信申请阶段
          data2.credit_apply
         when data1.typeMark = 'TRADEINFO' and data1.stage = '2' then -- 授信申请阶段对应的是交易授信申请阶段
          data2.credit_apply
         when data1.typeMark = 'TRADEINFO' and data1.stage = '3' then -- 授信获批阶段对应的是交易授信获批阶段
          data2.credit_approve
         when data1.typeMark = 'TRADEINFO' and data1.stage = '4' then -- 授信使用阶段对应的是交易授信使用阶段
          data2.credit_start
         when data1.typeMark = 'TRADEINFO' and data1.stage = '5' then -- 授信到期阶段对应的是交易授信到期阶段
          data2.credit_end
         when data1.typeMark = 'TRADEINFO' and data1.stage = '6' then -- 结束阶段对应的是交易授信到期阶段
          data2.credit_end
       -- 交易机会 (债券)
         when data1.typeMark = 'TRADINGOPPORTUNITY' and data1.stage = '1' then -- 开始阶段对应的是交易机会沟通阶段
          data2.commu_trading_opportunity
         when data1.typeMark = 'TRADINGOPPORTUNITY' and data1.stage = '2' then -- 沟通阶段对应的是交易机会沟通阶段
          data2.commu_trading_opportunity
         when data1.typeMark = 'TRADINGOPPORTUNITY' and data1.stage = '3' then -- 立项阶段对应的是交易机会立项阶段
          data2.init_trading_opportunity
         when data1.typeMark = 'TRADINGOPPORTUNITY' and data1.stage = '4' then -- 结束阶段对应的是交易机会立项阶段
          data2.init_trading_opportunity
       -- 交易机会 (贷款)  
         when data1.typeMark = 'TRADINGOPPORTUNITYLOAN' and
              data1.stage in ('1', '2', '3') then -- 开始、沟通、结束 阶段对应的是交易机会沟通阶段
          data2.commu_trading_opportunity
       -- 租期检查
         when data1.typeMark = 'LEASECHECK' and
              datas.entity_id =
              (select tsl.id
                 from T_STL_S_LEASE_CHECK tsl, T_STL_S_LEASE_CHECK_USER tslu
                where tsl.id = datas.entity_id
                  and tslu.lease_check_id = tsl.id
                  and tslu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '1' then -- 开始阶段对应的是租期检测的计划阶段
          data2.plan_lease_check
         when data1.typeMark = 'LEASECHECK' and
              datas.entity_id =
              (select tsl.id
                 from T_STL_S_LEASE_CHECK tsl, T_STL_S_LEASE_CHECK_USER tslu
                where tsl.id = datas.entity_id
                  and tslu.lease_check_id = tsl.id
                  and tslu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '2' then -- 计划阶段对应的是租期检测的计划阶段
          data2.plan_lease_check
         when data1.typeMark = 'LEASECHECK' and
              datas.entity_id =
              (select tsl.id
                 from T_STL_S_LEASE_CHECK tsl, T_STL_S_LEASE_CHECK_USER tslu
                where tsl.id = datas.entity_id
                  and tslu.lease_check_id = tsl.id
                  and tslu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '3' then -- 执行阶段对应的是租期检测的执行阶段
          data2.execute_lease_check
         when data1.typeMark = 'LEASECHECK' and
              datas.entity_id =
              (select tsl.id
                 from T_STL_S_LEASE_CHECK tsl, T_STL_S_LEASE_CHECK_USER tslu
                where tsl.id = datas.entity_id
                  and tslu.lease_check_id = tsl.id
                  and tslu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '4' then -- 分析阶段对应的是租期检测的分析阶段
          data2.analysis_lease_check
         when data1.typeMark = 'LEASECHECK' and
              datas.entity_id =
              (select tsl.id
                 from T_STL_S_LEASE_CHECK tsl, T_STL_S_LEASE_CHECK_USER tslu
                where tsl.id = datas.entity_id
                  and tslu.lease_check_id = tsl.id
                  and tslu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '5' then -- 分析阶段对应的是租期检测的分析阶段
          data2.analysis_lease_check
       --质量分类
         when data1.typeMark = 'QUALITYCLASSIFY' and
              datas.entity_id =
              (select tsq.id
                 from T_STL_S_QUALITY_CLASSIFY      tsq,
                      T_STL_S_QUALITY_CLASSIFY_USER tsqu
                where tsq.id = datas.entity_id
                  and tsqu.quality_classify_id = tsq.id
                  and tsqu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '1' then -- 开始阶段对应的是质量分类的计划阶段
          data2.plan_quality_classify
         when data1.typeMark = 'QUALITYCLASSIFY' and
              datas.entity_id =
              (select tsq.id
                 from T_STL_S_QUALITY_CLASSIFY      tsq,
                      T_STL_S_QUALITY_CLASSIFY_USER tsqu
                where tsq.id = datas.entity_id
                  and tsqu.quality_classify_id = tsq.id
                  and tsqu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '2' then -- 计划阶段对应的是质量分类的计划阶段
          data2.plan_quality_classify
         when data1.typeMark = 'QUALITYCLASSIFY' and
              datas.entity_id =
              (select tsq.id
                 from T_STL_S_QUALITY_CLASSIFY      tsq,
                      T_STL_S_QUALITY_CLASSIFY_USER tsqu
                where tsq.id = datas.entity_id
                  and tsqu.quality_classify_id = tsq.id
                  and tsqu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '3' then -- 初分阶段对应的是质量分类的初分阶段
          data2.analysis_quality_classify
         when data1.typeMark = 'QUALITYCLASSIFY' and
              datas.entity_id =
              (select tsq.id
                 from T_STL_S_QUALITY_CLASSIFY      tsq,
                      T_STL_S_QUALITY_CLASSIFY_USER tsqu
                where tsq.id = datas.entity_id
                  and tsqu.quality_classify_id = tsq.id
                  and tsqu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '4' then -- 终分阶段对应的是质量分类的终分阶段
          data2.approve_quality_classify
         when data1.typeMark = 'QUALITYCLASSIFY' and
              datas.entity_id =
              (select tsq.id
                 from T_STL_S_QUALITY_CLASSIFY      tsq,
                      T_STL_S_QUALITY_CLASSIFY_USER tsqu
                where tsq.id = datas.entity_id
                  and tsqu.quality_classify_id = tsq.id
                  and tsqu.user_id = '4028ef8156a07f530156a08b3cb3010b') and
              data1.stage = '5' then -- 结束阶段对应的是质量分类的终分阶段
          data2.approve_quality_classify
       -- 如果上面的条件不满足 则不应该展示数据  
         else
          2
       end) = 1
 order by datas.create_date desc;


 
 

你可能感兴趣的:(数据库)