同一列多次出​​现的数值

Numerical Value That Appears Several Times in Same Column

所以我有一个 table,员工。我想显示部门 50、60 和 80 雇用员工的同一个月。这是我写的代码:

    select to_char(hire_date, 'Month') as "Month Name" 
    from employees
    where department_id = any (50, 60, 80);

但是,这会显示三个部门雇用员工的所有月份:

    Month Name
    January
    May
    February
    November
    October
    January
    March
    July
    January
    May
    March

由于一月是所有三个部门中出现的同一个月,我只想显示一月。我该如何解决?

你可以统计实际匹配到多少部门,也就是说你需要按月分组;然后过滤那些与您要查找的部门数量相同的匹配项:

select to_char(hire_date, 'Month') as "Month Name" 
from employees
where department_id = any (50, 60, 80)
group by to_char(hire_date, 'Month')
having count(distinct department_id) = 3;

Month Name                          
------------------------------------
January  
February 
May      

那是在寻找任何一年中的同一个月 - 我想这可能有助于查看谁有周年纪念日。如果你真的想要相同的实际月份,在同一年,那么分组需要包括年份:

select to_char(trunc(hire_date, 'MM'), 'Month') as "Month Name" 
from employees
where department_id = any (50, 60, 80)
group by trunc(hire_date, 'MM')
having count(distinct department_id) = 3;

Month Name                          
------------------------------------
February 

这是标准 HR 模式的员工 table;如果您一直在修改它,您可能会有不同的数据。在此数据中,其中一个部门中有 12 人的雇用日期在任何 2 月; 1999 年 2 月聘用了三名,每个部门各一名。