案例:oracle中case when的用法
例子1、最近支援同事做報表,特提供資料來源的sql指令碼,當中就用到了case when。主要的檢視結構如下:
create or replace view v_card_channel_count5
(
icpcode,sumarea,count1201,count1202,count1203,count1204,count1205,
count1206,count1207,count1208,count1209,count1210,count1211,count1212,
count1213,count1214,count1215,count1216,count1217,count1218,count1219,count1220,
count1221,count1222,count1223,count1224,count1225,count1226,count1227,count1228,
count1229,count1230,count1231)
as
select icpcode,'當期訂購數',
sum(case when to_char(subscribe_time,'dd')='01' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='02' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='03' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='04' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='05' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='06' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='07' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='08' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='09' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='10' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='11' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='12' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='13' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='14' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='15' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='16' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='17' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='18' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='19' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='20' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='21' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='22' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='23' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='24' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='25' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='26' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='27' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='28' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='29' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='30' then subscribe_num else 0 end),
sum(case when to_char(subscribe_time,'dd')='31' then subscribe_num else 0 end)
from temp_card_service_subscribe
where to_char(subscribe_time,'yyyymm')='200612'
group by icpcode
union all
select icpcode,'當期資源總數',
count(distinct case when to_char(subscribe_time,'dd')='01' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='02' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='03' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='04' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='05' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='06' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='07' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='08' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='09' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='10' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='11' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='12' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='13' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='14' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='15' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='16' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='17' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='18' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='19' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='20' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='21' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='22' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='23' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='24' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='25' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='26' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='27' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='28' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='29' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='30' then contentid else null end),
count(distinct case when to_char(subscribe_time,'dd')='31' then contentid else null end)
from temp_card_service_subscribe
where to_char(subscribe_time,'yyyymm')='200612'
group by icpcode;
另外一個例子是最佳化sql語句的,見如下:
(原sql語句)
select f.subject_id from subject_data_common f
where validat ='A' and template_id = 1
and f.miscid in ('999')
and f.subject_id in (select subject_id
from ( select -1 as subject_id from dual
union all
select subject_id from subject_data where field_id= 3 and field_value in ('wu')
union all
select subject_id from subject_data where field_id = 4 and field_value in ('money')
union all
select subject_id from subject_data where field_id =7 and field_value in ('1')
union all
select subject_id from subject_data_common
where instr(',' || keyword || ',',',財富彩票投資,') > 0
)
group by subject_id
having count(subject_id) >= 4
)
order by f.createdate asc;
(從減少表遍歷和利用索引上考慮如下改進的sql語句)
select f.subject_id from subject_data_common f,(select subject_id
from ( select -1 as subject_id from dual
union all
select case
when field_id= 3 and field_value in ('wu') then subject_id
when field_id= 4 and field_value in ('money') then subject_id
when field_id= 7 and field_value in ('1') then subject_id
else null
end
from subject_data
union all
select subject_id from subject_data_common
where instr(',' || keyword || ',',',財富彩票投資,') > 0
)
group by subject_id
having count(subject_id) >= 4) e
where validat ='A' and template_id = 1
and f.miscid in ('999')
and f.subject_id = e.subject_id
order by f.createdate asc;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/38542/viewspace-886096/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORACLE CASE WHEN 及 SELECT CASE WHEN的用法Oracle
- SQL中的case when then else end用法SQL
- oracle面試題[關於case when的用法]Oracle面試題
- Oracle Case WhenOracle
- Oracle 中 case的用法Oracle
- Oracle的order by case whenOracle
- sql server select case when的用法SQLServer
- SQL中的case when then else end用法 【詳細】轉載SQL
- 【專案實戰】---SQLServer中case when的簡單用法SQLServer
- mysql中case when的使用MySql
- mysql中的case when 與if()MySql
- SQL中的CASE WHEN使用SQL
- oracle plsql case when_end case小記OracleSQL
- Oracle case when改寫SQLOracleSQL
- ORACLE多欄位CASE WHENOracle
- SQLServer使用case when中的order bySQLServer
- sql中case when的小學SQL
- SQL Case WhenSQL
- 轉 sql 中 case 的用法SQL
- Oracle Case語句用法Oracle
- mysql case when then 使用MySql
- Oracle vs PostgreSQL Develop(30) - Index&Case whenOracleSQLdevIndex
- ORACLE SQL開發where子句之case-whenOracleSQL
- Case when 支援變數變數
- case when遇上null值Null
- SQL Case when 的使用方法SQL
- oracle case處理案例(一)Oracle
- ORA-00937——Oracle中GROUP BY搭配CASE WHEN的一則SQL報錯OracleSQL
- Oracle 條件索引 case when 報錯解決方案Oracle索引
- sqlserver與oracle case when else ,isnull語法差別SQLServerOracleNull
- PL/SQL Case when應用SQL
- sql case when, Exist ,group by ,聚合SQL
- SQL Server CASE WHEN ... THEN ... ELSE ... ENDSQLServer
- plsql_case when_end case學習小例SQL
- plsql_case when_if else endifSQL
- MySQL 的CASE WHEN 語句使用說明MySql
- MySQL中case?when對NULL值判斷的踩坑記錄RTGYMySqlNull
- Oracle group by與case when統一單位後統計數量Oracle