简体   繁体   中英

SQL [Oracle] comparing multiple rows

I have a problem... I have table with informations about employees and I have to select sections, where is more employees without title then with ING title. How can i do that ? Why cant I use this mine SQL code ? It says single-row subquery returns more than one row. But i need to compare more than 1 row.

Thank for help :)

select section_number from employe where
 (select count(section_number) from employe where title='ING' group by section_number) 
> 
(select count(section_number) from employe where title IS NULL group by section_number) 
group by section_number;

You can use the below select

With ing_title as (select count(1) sec_ing,
section_number from employe where title='ING' 
group by section_number),

null_title as ( select count(1) sec_null,
section_number from employe where title is null group by section_number)

select ing_title.section_number 
from ing_title,null_title
where ing_title.sec_ing > null_title.sec_null
and ing_title.section_number=null_title.section_number

according to descriptions in the question, I think you need this:

select count(section_number),title 
from employe 
where title is null or title='ING' 
group by title 
Order by title nulls first

it will give you number of employees with no title and number of employees with title='ING'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM