mysql - SQL SELECT Query -
suppose have sql table "company" 3 columns: "department_id", "employee", "job". this:
departament_id | employee | job -------------------------------------- 1 | mark | president 1 | robert | marketing manager 1 | rose | administration assitant 2 | anna | programmer 2 | michael | programmer 2 | celia | sales manager 3 | jhon | sales manager 3 | donna | programmer 3 | david | marketing manager
i write query returns departments id @ least 50% of jobs same.
result need in example just:
departament_id | -------------------------------------- 2 |
how write sql query? think tried kind of stuff dont :(.
this bit tricky. need compare total number of people on job in department total number. so, 1 method uses 2 aggregations:
select department_id (select department_id, count(*) numemp t group department_id ) d join (select department_id, max(numemp) numemp (select department_id, job, count(*) numemp t group department_id, job ) d group department_id ) dj on d.numemp <= 2 * dj.numemp;
you might duplicates if have 1 department split between 2 jobs. in case, use select distinct
.
Comments
Post a Comment