查找具有最大部门数的部门区号。 我尝试使用我的代码,但无法正常工作
select department_block_number
from department
where department_id in (select max(department_id)
from department
group by department_block_number);
上面的代码显示了2个值的输出
202
303
而预期输出是
的单个值303
请帮助
答案 0 :(得分:1)
您需要汇总!
select department_block_number
from department
group by department_block_number
having count(*) = (select max(cnt)
from (select count(*) as cnt
from department
group by department_block_number
) d
);
还有其他表达这种逻辑的方法,但这就是您似乎正在尝试的方法。
答案 1 :(得分:1)
我找到了一个解决方案,这可能是硬编码的,但我通过了测试用例
select department_block_number from department
where department_id in (select min(count(*)) from department
group by department_block_number)
group by department_block_number;
答案 2 :(得分:0)
您可以使用以下内容满足要求:
SQL> select * from department;
DEPARTMENT_ID DEPARTMENT_BLOCK_NUMBER
------------- -----------------------
1 303
2 303
3 303
4 303
5 202
6 202
-- Your Query
SELECT
DEPARTMENT_BLOCK_NUMBER,
COUNT(1)
FROM
DEPARTMENT
GROUP BY
DEPARTMENT_BLOCK_NUMBER
ORDER BY
2 DESC NULLS LAST
FETCH FIRST ROW ONLY;
查询结果:
DEPARTMENT_BLOCK_NUMBER COUNT(1)
----------------------- ----------
303 4
您可以根据需要删除计数列。
答案 3 :(得分:0)
编写查询以显示最大部门数所在的街区号。
select department_block_number
from department where
department_id in (
select max(count(department_id))
from depratment
group by department_id
)
group by department_block_number;