编写查询以按部门名称显示具有最大人员计数顺序的部门的名称

时间:2019-07-15 18:23:39

标签: sql oracle

import { NgxMaskModule, IConfig } from 'ngx-mask';

export let options: Partial | (() => Partial);

@NgModule({
  imports: [
  NgxMaskModule.forRoot(options)],
 }
})

select max(count(department_id)) 
from staff
group by department_id    
ERROR at line 4:                                                                
ORA-00918: column ambiguously defined                                           

---预期输出---

Select department_name 
from staff s 
  inner join department d on s.department_id=d.department_id 
having count(s.department_id) in (Select max(count(department_id)) 
                                  from staff) group by department_id

5 个答案:

答案 0 :(得分:0)

查询:

select dept.DEPARTMENT_NAME, count(dept.DEPARTMENT_NAME) as staff_count from 
hr.departments dept, hr.employees emp
where dept.DEPARTMENT_ID=emp.DEPARTMENT_ID(+)
group by dept.DEPARTMENT_NAME
order by count(dept.DEPARTMENT_NAME) desc 
FETCH FIRST 1 ROW ONLY;

您可以在https://livesql.oracle.com/

中运行上述查询

有关Fetch的更多信息,请参见链接     https://oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1

答案 1 :(得分:0)

@Component
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(byteArrayHttpMessageConverter());
        converters.add(new ResourceHttpMessageConverter());
    }

    @Bean
    public HttpMessageConverter byteArrayHttpMessageConverter() {
        ByteArrayHttpMessageConverter arrayHttpMessageConverter =
                new ByteArrayHttpMessageConverter();
        arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
        return arrayHttpMessageConverter;
    }

    private List<MediaType> getSupportedMediaTypes() {
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_PDF);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);

        return mediaTypes;
    }
}

答案 2 :(得分:0)

在多个地方,您错过了别名,我将其添加为以下内容:

SELECT
    D.DEPARTMENT_NAME -- ADDED ALIAS HERE
FROM
    STAFF S
    INNER JOIN DEPARTMENT D ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
GROUP BY
    D.DEPARTMENT_ID -- ADDED ALIAS HERE
HAVING
    COUNT(S.DEPARTMENT_ID) IN (
        SELECT
            MAX(COUNT(DEPARTMENT_ID))
        FROM
            STAFF
    );

此外,您可以使用以下查询获得相同的结果:

SELECT
    D.DEPARTMENT_NAME
FROM
    DEPARTMENT D
    JOIN (
        SELECT
            S.DEPARTMENT_ID,
            COUNT(1)
        FROM
            STAFF S
        ORDER BY
            2 DESC
        FETCH FIRST ROWS ONLY
    ) S ON S.DEPARTMENT_ID = D.DEPARTMENT_ID;

干杯!

答案 3 :(得分:0)

检查一下

select department_name from (select d.department_name,count(s.staff_id) as c from department d join staff s on d.department_id=s.department_id
group by d.department_name) mx where c=(select max(count(staff_id)) from staff group by department_id) order by department_name;

答案 4 :(得分:-2)

SELECT department_name
FROM department
WHERE department_id IN
    (SELECT department_id
     FROM staff
     HAVING COUNT(department_id) IN
       (SELECT MAX(COUNT(department_id))
        FROM staff
        GROUP BY department_id)
     GROUP BY department_id);