How to give RANKs based on employee hierarchy in emp table - mysql

I have emp table and KING is the CEO of the company, he does not have a manager.
KING should get RANK '1'.
KING is the manager for JONES,CLARK,BLAKE. So these will get RANK '2'....etc.
intermediate table for emp and manager relation:
empno ename mgr
7566 JONES KING
7782 CLARK KING
7698 BLAKE KING
7900 JAMES BLAKE
7844 TURNER BLAKE
7654 MARTIN BLAKE
7521 WARD BLAKE
7499 ALLEN BLAKE
7934 MILLER CLARK
7902 FORD JONES
7788 SCOTT JONES
7876 ADAMS SCOTT
7369 SMITH FORD
7839 KING NULL
Final output should be like:
mgr rank
KING 1
JONES 2
CLARK 2
BLAKE 2
JAMES 3
TURNER 3
MARTIN 3
WARD 3
ALLEN 3
MILLER 3
SCOTT 3
FORD 3
ADAMS 4
SMITH 4
Thanks in advance. I want this to be done in oracle.

It took me while to wrap my head around the heirarchy queries in the past ... however, once you take the time to fiddle with them, they usually aren't that tough.
SQL> with w_data as (
2 select 7566 empno, rtrim('JONES ') ename, rtrim('KING ') mgr from dual union all
3 select 7782 empno, rtrim('CLARK ') ename, rtrim('KING ') mgr from dual union all
4 select 7698 empno, rtrim('BLAKE ') ename, rtrim('KING ') mgr from dual union all
5 select 7900 empno, rtrim('JAMES ') ename, rtrim('BLAKE') mgr from dual union all
6 select 7844 empno, rtrim('TURNER') ename, rtrim('BLAKE') mgr from dual union all
7 select 7654 empno, rtrim('MARTIN') ename, rtrim('BLAKE') mgr from dual union all
8 select 7521 empno, rtrim('WARD ') ename, rtrim('BLAKE') mgr from dual union all
9 select 7499 empno, rtrim('ALLEN ') ename, rtrim('BLAKE') mgr from dual union all
10 select 7934 empno, rtrim('MILLER') ename, rtrim('CLARK') mgr from dual union all
11 select 7902 empno, rtrim('FORD ') ename, rtrim('JONES') mgr from dual union all
12 select 7788 empno, rtrim('SCOTT ') ename, rtrim('JONES') mgr from dual union all
13 select 7876 empno, rtrim('ADAMS ') ename, rtrim('SCOTT') mgr from dual union all
14 select 7369 empno, rtrim('SMITH ') ename, rtrim('FORD ') mgr from dual union all
15 select 7839 empno, rtrim('KING ') ename, NULL mgr from dual
16 )
17 select empno, ename, mgr, level
18 from w_data
19 connect by mgr = PRIOR ename
20 start with mgr IS NULL
/
21
EMPNO ENAME MGR LEVEL
---------- ------ ----- ----------
7839 KING 1
7698 BLAKE KING 2
7499 ALLEN BLAKE 3
7900 JAMES BLAKE 3
7654 MARTIN BLAKE 3
7844 TURNER BLAKE 3
7521 WARD BLAKE 3
7782 CLARK KING 2
7934 MILLER CLARK 3
7566 JONES KING 2
7902 FORD JONES 3
EMPNO ENAME MGR LEVEL
---------- ------ ----- ----------
7369 SMITH FORD 4
7788 SCOTT JONES 3
7876 ADAMS SCOTT 4
14 rows selected.
SQL>
w_data is just to fake out your test data ..
connect by mgr = prior ename
this takes each record, and compares the mgr with the previous level employee.
start with mgr is null
this tells Oracle where to start ...
Level is the depth of the heirarchy.

I believe Oracle supports CASE WHERE in the ORDER BY clause. Something like this:
SELECT [blah]
FROM [intermediate tbl]
ORDER BY
CASE WHEN mgr IS NULL THEN 1
CASE WHEN mgr = 'KING' THEN 2
... ELSE ... END
I believe there is a shorter way to do this in Oracle (my experience is with SSMS):
ORDER BY decode(mgr, 'NULL', 1, 'KING', 2, ...)

Related

Why COUNT in the SQL HAVING condition doesn't calculate correct value?

When i solve this HackerRank problem
i found that my solution with condition
COUNT(total) = 1 doesn't work:
SELECT H.hacker_id AS hacker_id, H.name AS name, COUNT(C.challenge_id) AS total
FROM Hackers H
JOIN Challenges C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING
COUNT(total) = 1 OR -- HERE
OR
total = (
SELECT MAX(amount) FROM (
SELECT COUNT(challenge_id) AS amount FROM Challenges
GROUP BY hacker_id
) t
)
ORDER BY total DESC, hacker_id;
Output:
5120 Julia 50
18425 Anna 50
20023 Brian 50
33625 Jason 50
41805 Benjamin 50
52462 Nicholas 50
64036 Craig 50
69471 Michelle 50
77173 Mildred 50
94278 Dennis 50
96009 Russell 50
96716 Emily 50
2689 Joe 1
3432 Linda 1
5827 Kelly 1
6011 Robin 1
7537 David 1
7961 Michelle 1
9642 Joseph 1
9901 Lawrence 1
10975 Christine 1
11715 Louise 1
13075 John 1
13905 Evelyn 1
14307 David 1
14726 Emily 1
15109 Dorothy 1
17282 Norma 1
17311 Andrew 1
17663 Benjamin 1
17846 Alan 1
18709 James 1
19032 Andrew 1
19781 Jesse 1
19962 Patricia 1
20190 Aaron 1
21350 Bobby 1
21821 Carol 1
21916 Clarence 1
22396 Wayne 1
22455 Carolyn 1
23812 Jerry 1
24047 Elizabeth 1
25684 Alan 1
25990 Mildred 1
26802 Paul 1
27797 Helen 1
28766 Paul 1
29242 Jennifer 1
29841 Charles 1
30677 Keith 1
30778 Jose 1
30908 Helen 1
31770 Ashley 1
32364 Julia 1
32735 Cheryl 1
33273 Sara 1
33489 Denise 1
37092 Jennifer 1
37764 Jimmy 1
38540 Katherine 1
42467 Ernest 1
43240 Diana 1
43398 Steve 1
43595 Adam 1
45685 Bobby 1
46144 Sharon 1
46468 Timothy 1
46604 Christina 1
47156 Kelly 1
47921 Lillian 1
50560 Brian 1
52350 Teresa 1
53451 Jeffrey 1
53597 Rose 1
53768 Douglas 1
54015 Carolyn 1
54510 Paula 1
55415 Amy 1
56039 Teresa 1
56103 Kelly 1
56338 Jose 1
57195 Beverly 1
57873 Diana 1
58086 Debra 1
58167 David 1
58543 Rachel 1
59871 Martin 1
59895 Martha 1
60177 Brian 1
61093 Mark 1
61102 Kenneth 1
61206 Lillian 1
61769 Marie 1
63263 Dorothy 1
63684 Randy 1
63730 Sarah 1
63803 Carolyn 1
63961 Anna 1
64341 Virginia 1
64882 Roy 1
68178 Gloria 1
70499 Dennis 1
72321 Julie 1
72763 Julie 1
73267 Jeremy 1
73676 Linda 1
74320 Pamela 1
78615 Kathryn 1
79612 Tina 1
81652 Albert 1
83308 Roy 1
84739 Alan 1
84938 Judy 1
85094 Matthew 1
86142 Douglas 1
87040 Craig 1
87885 Gregory 1
88069 Jean 1
88083 Anna 1
88084 Alan 1
88858 Bruce 1
89514 Jeffrey 1
89903 Katherine 1
90276 Joyce 1
90369 Christina 1
91620 Debra 1
92239 Shirley 1
92920 Louis 1
94337 Lillian 1
94676 Patrick 1
94746 Adam 1
96521 Christine 1
96773 Angela 1
97338 Amy 1
98785 Rose 1
99101 Timothy 1
99165 Nancy 1
But this works:
SELECT H.hacker_id AS hacker_id, H.name AS name, COUNT(C.challenge_id) AS total
FROM Hackers H
JOIN Challenges C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING
--
total IN
(SELECT t0.total
FROM
(SELECT count(*) AS total
FROM challenges
GROUP BY hacker_id) t0
GROUP BY t0.total
HAVING count(t0.total) = 1)
-- instead of 'COUNT(total) = 1'
OR
total = (
SELECT MAX(amount) FROM (
SELECT COUNT(challenge_id) AS amount FROM Challenges
GROUP BY hacker_id
) t
)
ORDER BY total DESC, hacker_id;
Output:
5120 Julia 50
18425 Anna 50
20023 Brian 50
33625 Jason 50
41805 Benjamin 50
52462 Nicholas 50
64036 Craig 50
69471 Michelle 50
77173 Mildred 50
94278 Dennis 50
96009 Russell 50
96716 Emily 50
72866 Eugene 42
37068 Patrick 41
12766 Jacqueline 40
86280 Beverly 37
19835 Joyce 36
38316 Walter 35
29483 Jeffrey 34
23428 Arthur 33
95437 George 32
46963 Barbara 31
87524 Norma 30
84085 Johnny 29
39582 Maria 28
65843 Thomas 27
5443 Paul 26
52965 Bobby 25
77105 Diana 24
33787 Susan 23
45855 Clarence 22
33177 Jane 21
7302 Victor 20
54461 Janet 19
42277 Sara 18
99388 Mary 16
31426 Carlos 15
95010 Victor 14
27071 Gerald 10
90267 Edward 9
72609 Bobby 8
So, why i can't use just this
COUNT(total) = 1
instead of this large condition:
total IN
(SELECT t0.total
FROM
(SELECT count(*) AS total
FROM challenges
GROUP BY hacker_id) t0
GROUP BY t0.total
HAVING count(t0.total) = 1)
Because in your context total is just an alias to a aggregate result, not a value. In general you must repeat the COUNT(C.challenge_id) part but it will not help you here as COUNT(COUNT(C.challenge_id)) = 1 is clear wrong.
There are solutions to it like putting the aggregation dataset result in a temp table/table variable or using a CTE, that's is why a subquery works.
Disclaimer: don't chequed your query for correctness but that IN sounds ugly and using a CTE can possible be a better approach.
Below an example solution using MS SQL, sorry, don't get a MySql running right now nut don't used CTE =)
create table dbo.HACKER
(
hacker_id int,
name varchar(100)
)
GO
create table dbo.CHALLENGE
(
challenge_id int,
hacker_id int
)
GO
insert into dbo.HACKER
(hacker_id,name)
values
(5077,'Rose')
,(21283,'Angela')
,(62743,'Frank')
,(88255,'Patrick')
,(96196,'Lisa')
insert into dbo.CHALLENGE
(challenge_id, hacker_id)
values
(61654,5077)
,(58302,21283)
GO
--drop table #Temp
--drop table #totalsToExclude
select hk.hacker_id, hk.name, x.total
into #Temp
from dbo.HACKER hk
join (select ch.hacker_id, count(*) as total from dbo.CHALLENGE ch group by ch.hacker_id) as x
on x.hacker_id = hk.hacker_id
select * from #Temp
declare #maxTotal as int =(select max(total) from #temp)
select #maxTotal
select t.total, count(*) as [Count_total]
into #totalsToExclude
from #temp t
group by t.total
having(count(*) >1)
delete tx from #totalsToExclude tx where tx.total = #maxTotal
select * from #totalsToExclude
select * from #Temp t
where t.total not in (select t.total from #totalsToExclude)
order by t.total desc, t.hacker_id

Sql Query to print ename and dname please share me how to write query in oracle or Mysql

Employee
Eid ename salary
1 charles 3000
2 kiran 2000
3 naveen 8000
4 manju 7000
Department
deptid dname
101 charles
102 kiran
103 naveen
104 manju
Register
deptid eid
101 1
102 2
103 3
104 4
write a query to print (employee) ename and (department)dname.
SELECT a.ename, b.depart, c.eid
FROM employee a
INNER JOIN department b on a.ename=b.dname
INNER JOIN register c on b.deptid=c.deptid
where a.ename=b.dname and b.deptid=c.deptid;

Mysql unable to get aggregate query

I have a table with values as follows(MySql):
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
but I need them in the following less convenient format:
DEPTNO ENAME
------ -----------------------------------------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
Kindly help me to achiever above result/output.
Using group_concat
SELECT deptno, group_concat(ename order by ename) as ename
FROM emp
group by deptno
ORDER BY deptno;

Count Query with Join

i have table as
Dept
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
and another table as
Emp
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 12/17/1980 800 NULL 20
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30
7566 JONES MANAGER 7839 4/2/1981 2975 NULL 20
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30
7698 BLAKE MANAGER 7839 5/1/1981 2850 NULL 30
7782 CLARK MANAGER 7839 6/9/1981 2450 NULL 10
7788 SCOTT ANALYST 7566 12/9/1982 3000 NULL 20
7839 KING PRESIDENT NULL 11/17/1981 5000 NULL 10
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30
7876 ADAMS CLERK 7788 1/12/1983 1100 NULL 20
7900 JAMES CLERK 7698 12/3/1981 950 NULL 30
7902 FORD ANALYST 7566 12/3/1981 3000 NULL 20
7934 MILLER CLERK 7782 1/23/1982 1300 NULL 10
My question : List ALL the department names and their employee count.
Count should include only those employees whose hire date is greater than 1981
Result: should be like this
DNAME EMPCOUNT
ACCOUNTING 1
OPERATIONS 0
RESEARCH 2
SALES 0
please help
SELECT DNAME,
count(emp.deptno) AS EMPCOUNT
FROM DEPT
LEFT JOIN emp ON DEPT.DEPTNO = emp.DEPTNO
WHERE YEAR(hiredate)>1981
GROUP BY emp.deptno

MySQL - Get the year when there are most registered

I have this table (named EMP) in MySQL:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ----- ------- ---- -------- -------- ------- ------
7369 SMITH CLERK 7902 13-JUN-83 800.00 20
7499 ALLEN SALESMAN 7698 15-AUG-83 1,600.00 300.00 30
7521 WARD SALESMAN 7698 26-MAR-84 1,250.00 500.00 30
7566 JONES MANAGER 7839 31-OCT-83 2,975.00 20
7654 MARTIN SALESMAN 7698 05-DEC-83 1,250.00 1,400.00 30
7698 BLAKE MANAGER 7839 11-JUL-84 2,850.00 30
7782 CLARK MANAGER 7839 14-MAY-84 2,450.00 10
7788 SCOTT ANALYST 7566 05-MAR-84 3,000.00 20
7839 KING PRESIDENT 09-JUL-84 5,000.00 10
7844 TURNER SALESMAN 7698 04-JUN-84 1,500.00 .00 30
7876 ADAMS CLERK 7788 04-JUN-84 1,100.00 20
7900 JAMES CLERK 7698 23-JUL-84 950.00 30
7902 FORD ANALYST 7566 05-DEC-83 3,000.00 20
7934 MILLER CLERK 7782 21-NOV-83 1,300.00 10
How can I write a Select query that returns the year (HIREDATE column) when there are most number of employees (rows) registered?
What about
SELECT YEAR(hiredate) FROM emp
GROUP BY YEAR(hiredate)
ORDER BY COUNT(*) DESC
LIMIT 1
?
What about:
SELECT COUNT(1), YEAR(HIREDATE) FROM EMP GROUP BY YEAR(HIREDATE);
TRY
SELECT count(*) as total FROM emp GROUP BY hiredate ORDER BY total DESC LIMIT 1;
Try this:
select year(hiredate), count(*) from emp
group by year(hiredate) order by 2 desc limit 1