Mysql 计数SQL语句而不创建其他视图

Mysql 计数SQL语句而不创建其他视图,mysql,sql,oracle,Mysql,Sql,Oracle,我有一个表名为JOB_Details,数据显示如下: Employee_ID Age Department Gender 001 30yrs IT M 002 34yrs HR F 003 39yrs HR F 004

我有一个表名为JOB_Details,数据显示如下:

 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
我希望显示的输出应该如下

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 
年龄部门:IT部门:财务部门:人力资源总计
男女男女
选择
如果年龄<30岁,则选择“


如果年龄<30岁,则使用MySQL版本。无法在SQL Server或Oracle中工作

不带外部表或视图的Oracle版本:

年龄范围为(
选择0作为“底部”,选择29作为“顶部”,选择MySQL版本。在SQL Server和Oracle中都不起作用

不带外部表或视图的Oracle版本:

年龄范围为(
选择0作为“底部”,选择29作为“顶部”,“您好,您的建议很好。正如我的反馈,但它在oracle SQL中不起作用。无论如何,非常感谢您。@goh6319那么它是oracle,而不是MySQL,也不是标签中的SQL Server?@goh6319添加了oracle版本,我只需要这样做的概念。但您已经为我提供了一个很好的解决方案。非常感谢。您好,您的建议很好。就像我的feedbac一样。”k但它在oracle SQL中不起作用。无论如何,非常感谢。@goh6319那么它是oracle,而不是MySQL,也不是标签中的SQL Server?@goh6319添加了oracle versioni,只是需要这样做的概念。但是你已经为我提供了一个很好的解决方案。非常感谢。嗨,Zgravko,谢谢你的建议。但是你的解决方案只能显示总计。假设我希望显示每个数量的男性和女性。如何执行此操作?(^^)而不创建新视图。SUM(当Department='it'和Gender='M'时,情况为1,否则为0)结束即是M,您好,Zgravko,谢谢您的建议。但是您的解决方案只能显示总数。假设我希望显示每个男性和女性的数字。您如何执行它?(^^)而不创建新视图。SUM(当Department='IT'和Gender='M'时为CASE,然后为1,否则为0)结束即是IT,
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
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
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