2012-10-12 52 views
1

我有一個表名作爲JOB_Details和數據顯示如下:像基於計數SQL語句沒有造成其他瀏覽

Age   Department:IT  Department:Finance Department:HR Total 
      Male  Female  Male  Female  Male  Female 
<30yrs   1   0   0   1   1   1  4 
30-34yrs  0   0   0   1   0   1  2 
35-39yrs  0   0   0   0   0   1  1 
40-49yrs  1   0   1   0   0   0  2 
50-54yrs  1   0   0   0   0   0  1 
Total   3   0   1   2   1   3  10 

Employee_ID Age   Department  Gender  
    001  30yrs   IT    M 
    002  34yrs   HR    F  
    003  39yrs   HR    F 
    004  49yrs   Finance   M   
    005  54yrs   IT    M 
    006  20yrs   HR    M 
    007  24yrs   HR    F 
    008  33yrs   Finance   F  
    009  29yrs   Finance   F   
    010  44yrs   IT    M 

我希望顯示的輸出應該是我的知識,唯一的辦法可以通過創建另一種觀點並將它們逐一分組來完成。但我想知道的是,另一種方式來做到這一點,而無需創建其他視圖?如果有人有其他好的建議,我希望瞭解它。非常感謝你。

回答

1

MySQL版本。不會在SQL Server中工作,也不會在Oracle中工作。 SQL Fiddle

select *, IT_Male + IT_Female + Finance_Male + Finance_Female + HR_Male + HR_Female as Total 
from (
    select 
     ar.`range` as Age, 
     count(Department = 'IT' and Gender = 'M' or null) as IT_Male, 
     count(Department = 'IT' and Gender = 'F' or null) as IT_Female, 
     count(Department = 'Finance' and Gender = 'M' or null) as Finance_Male, 
     count(Department = 'Finance' and Gender = 'F' or null) as Finance_Female, 
     count(Department = 'HR' and Gender = 'M' or null) as HR_Male, 
     count(Department = 'HR' and Gender = 'F' or null) as HR_Female 
    from 
     JOB_Details jd 
     inner join 
     age_range ar on jd.Age between ar.bottom and ar.top 
    group by ar.`range` 
    order by ar.bottom 
) s 
union 
select 
    'Total', 
    count(Department = 'IT' and Gender = 'M' or null), 
    count(Department = 'IT' and Gender = 'F' or null), 
    count(Department = 'Finance' and Gender = 'M' or null), 
    count(Department = 'Finance' and Gender = 'F' or null), 
    count(Department = 'HR' and Gender = 'M' or null), 
    count(Department = 'HR' and Gender = 'F' or null), 
    count(*) 
from JOB_Details 

Oracle版本而無需外部表或視圖:SQL Fiddle

with age_range as (
    select 0 as "bottom", 29 as "top", '<30' as "range" from dual union 
    select 30, 34, '30-34' from dual union 
    select 35, 39, '35-59' from dual union 
    select 40, 49, '40-49' from dual union 
    select 50, 54, '50-54' from dual 
) 
select s.*, IT_Male + IT_Female + Finance_Male + Finance_Female + HR_Male + HR_Female as Total 
from (
    select 
     ar."range" as Age, 
     count(case when Department = 'IT' and Gender = 'M' then 1 end) as IT_Male, 
     count(case when Department = 'IT' and Gender = 'F' then 1 end) as IT_Female, 
     count(case when Department = 'Finance' and Gender = 'M' then 1 end) as Finance_Male, 
     count(case when Department = 'Finance' and Gender = 'F' then 1 end) as Finance_Female, 
     count(case when Department = 'HR' and Gender = 'M' then 1 end) as HR_Male, 
     count(case when Department = 'HR' and Gender = 'F' then 1 end) as HR_Female 
    from 
     JOB_Details jd 
     inner join 
     age_range ar on jd.Age between ar."bottom" and ar."top" 
    group by ar."range", ar."bottom" 
    order by ar."bottom" 
) s 
union 
select 
    'Total', 
    count(case when Department = 'IT' and Gender = 'M' then 1 end), 
    count(case when Department = 'IT' and Gender = 'F' then 1 end), 
    count(case when Department = 'Finance' and Gender = 'M' then 1 end), 
    count(case when Department = 'Finance' and Gender = 'F' then 1 end), 
    count(case when Department = 'HR' and Gender = 'M' then 1 end), 
    count(case when Department = 'HR' and Gender = 'F' then 1 end), 
    count(*) 
from JOB_Details 
+0

您好,您的建議是good.as我的反饋,但它並沒有在Oracle SQL.Anyway工作,非常感謝你。 – goh6319

+0

@ goh6319然後它是Oracle,而不是標籤中的MySQL,而不是SQL Server? –

+0

@ goh6319增加甲骨文 –

0
SELECT 
    CASE WHEN Age < 30 THEN '<30' 
     WHEN 30 <= Age AND Age < 34 THEN '30-34' 
     ... 
    END as Age, 

    SUM(CASE WHEN Department = 'IT' AND Gender = 'M' THEN 1 ELSE 0) END AS IT_M, 
    SUM(CASE WHEN Department = 'IT' AND Gender = 'F' THEN 1 ELSE 0) END AS IT_F, 
    ... 

FROM JOB_DETAILS 

GROUP BY 
    CASE WHEN Age < 30 THEN '<30y' 
     WHEN 30 <= Age AND Age < 34 THEN '30-34' 
     ... 
    END 
+0

嗨Zgravko,謝謝您的建議。但是你的解決方案只能顯示總數。讓我說我想顯示每個男性和女性的數目。你如何執行它? (^^)而不創建新視圖。 – goh6319

+0

SUM(CASE WHEN部= 'IT',性別= 'M' THEN 1 ELSE 0)END AS IT_M,< - 這將讓你在男性IT的數量,你需要那些爲每個部門/性別之一。 .. –

+0

哦〜我知道了,謝謝dude.i得到它了。(^^) – goh6319