1. 程式人生 > >ORACLE 分類統計符合各個條件的個數 (case when 的用法)

ORACLE 分類統計符合各個條件的個數 (case when 的用法)

假如資料量很大約1000萬條;寫一個你認為最高效的SQL,用一個SQL計算以下四種人:
fsalary>9999 and fage > 35
fsalary>9999 and fage < 35
fsalary <9999 and fage > 35
fsalary <9999 and fage < 35
每種員工的數量;
select sum(case when fsalary > 9999 and fage > 35
then 1
else 0end) as "fsalary>9999_fage>35",
sum(case when fsalary > 9999 and fage < 35
then 1
else 0
end) as "fsalary>9999_fage<35",
sum(case when fsalary < 9999 and fage > 35
then 1
else 0
end) as "fsalary<9999_fage>35",
sum(case when fsalary < 9999 and fage < 35
then 1
else 0
end) as "fsalary<9999_fage<35"
from empinfo;