Is it possible write this query without cursor - mysql

I am using mysql database
The schema of department is
Department Table
Deptid DeptName
1 CEO
2 HR
3 IT
4 Dev
5 QA
Employee table
Empid EmpName managerid deptid
1 E1 1
2 E2 1 2
3 E3 1 3
4 E4 3 4
5 E5 4 4
6 E6 4 4
7 E7 3 5
8 E8 7 5
9 E9 7 5
I need output as
deptid parentdept count
1 1
2 1 1
3 1 1
4 3 3
5 3 3
Parent department is department which belongs to manager of employee.
It means It department CEO is parent department of IT and HR because IT and HR managers report to CEO dept manager.
It means It department IT is parent department of Dev and QA because DEV and QA managers report to IT dept manager.
deptid parentdept count
CEO 1
HR CEO 1
IT CEO 1
DEV IT 3
QA IT 3
Tree representation of it is
CEO-(deptid 1)E1
HR-(deptid 2)E2 IT-(deptid 3)E3
DEV-(deptid 4)E4 QA(deptid 5)E7
E5-(deptid 4) E6-(deptid 4) E8-(deptid 5) E9-(deptid 5)

I thought the managerid shoudn't be equal to the any empid in the same department rows by your descriptions.
select A.deptid,B.parentdept,A.count from (select deptid,
count(*) as count from Employee group by deptid) as A
left join
(select t1.deptid, t3.deptid as parentdept from Employee t1,Employee t3 where t1.managerid not
in (select Empid from Employee t2 where t2.deptid=t1.deptid)
and t1.managerid=t3.empid) as B
on A.deptid=B.deptid;
if I missed something in the logic, apologies in advance.

Related

Putting a table result as a text column

I have two tables, the first one is a table of companies, and the second one is a table of shareholders we could say. The shareholder table can have upwards of a thousand shareholders.
I need to make a view/another table which has every shareholder name on a single text column along with the unique identifier of the company, the name and "fake" name of the company.
Table 1 Company:
ID CompanyID Name FantasyName
1 1 A Company 1
2 2 B Company 2
3 3 C Company 3
4 4 D Company 4
5 5 E Company 5
6 6 F Company 6
7 7 G Company 7
Table 2 Shareholders:
ID CompanyID Name
1 1 John 1
2 1 Peter 2
3 1 Gabriel Li
4 2 Raphael 3
5 2 Anderson 4
6 2 Michael 6
7 2 Angelina Jo
8 3 John 8
9 4 Beatrice
10 4 Scarlet
11 5 Scarlet
12 5 Logan
13 5 John 1
I tried to do this via Linq through C# but it hasn't been fast enough for my purpose, this table contains upwards of millions of companies.
The end result would look like this:
Table 3 Company and Shareholders:
ID CompanyID Name FantasyName Shareholders
1 1 A Company 1 John 1,Peter 2,Gabriel Li
2 2 B Company 2 Raphael 3, Anderson 4,Michael 6,Angelina Jo
3 3 C Company 3 John 8
4 4 D Company 4 Beatrice,Scarlet
5 5 E Company 5 Scarlet,Logan,John 1
...
All shareholders in a single TEXT field, with the company info accompanying it.
You can use aggregation and GROUP_CONCAT():
CREATE VIEW myview AS
SELECT
c.ID,
c.CompanyID,
c.Name,
c.FantasyName,
GROUP_CONCAT(s.Name) Shareholders
FROM
Company c
INNER JOIN Shareholders s ON s.CompanyID = c.CompanyID
GROUP BY
c.ID,
c.CompanyID,
c.Name,
c.FantasyName

Join two tables with Mapping and get result

I'm use HBase DB with phoenix, I have 3 tables in which two are Main table which have many to many relation ship between them which is mapped by the third table.
Employee
EmpID EmpName
1 Robert
2 John
3 Sansa
4 Ned
5 Tyrion
6 George
7 Daenerys
8 Arya
9 Cersie
10 Catelyn
Department
DepID DepName
1 Hardware
2 Software
3 Admin
4 HR
Department_Employee_Mapping
ID DepID EmpID
1 1 2
2 1 6
3 2 1
4 3 5
5 3 6
6 4 3
7 4 7
8 4 10
9 4 5
I want to get names and department of all the employees who are in Admin Department, but also I want the details of those employee who belong to other department and who do not belong to any of the departments and these data should appear as NULL in the resultset Only the value for admin department should appear and if the employee belong to multiple departments, the result set should contain the value the Admin department and the other one will be ignored, Result set will look like
Emp Name Dep Name
Robert NULL
John NULL
Sansa NULL
Ned NULL
Tyrion Admin
George Admin
Daenerys NULL
Arya NULL
Cersie NULL
Catelyn Admin
Use 2 LEFT JOIN and include the filtering on 'Admin' in the join against Department
SELECT DISTINCT e.empname, d.depname
FROM Employee e
LEFT JOIN Department_Employee_Mapping x ON x.empid = e.id
LEFT JOIN Departmentd ON d.id = x.depid AND d.id = 3

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;

Is it possible write this query without using cursor

I have already written above logic using c# code but I want to handle it at database query level as optimization.
I am using mysql database The schema of department is
Department Table
Deptid DeptName ParentDeptid
1 CEO -
2 HR 1
3 IT 1
4 Dev 3
5 QA 3
6 clerk 5
Employee table
Empid EmpName deptid
1 E1 1
2 E2 2
3 E3 3
4 E4 4
5 E5 4
6 E6 4
7 E7 5
8 E8 5
9 E9 5
10 E10 6
I need output as if I have selected deptid is 3
I want all descendent of dept 3.
It means for dept id 3
deptid parentdept count
3 1 1
4 3 3
5 3 3
6 5 1

Add total of 3 rows for specific id

I have three tables:
Students
-------------------------------------------------------------
studentId first last gender weight
-------------------------------------------------------------
1 John Doe m 185
2 John Doe2 m 130
3 John Doe3 m 250
Lifts
-------------------
liftId name
-------------------
1 Bench Press
2 Power Clean
3 Parallel Squat
4 Deadlift
5 Shoulder Press
StudentLifts
------------------------------------------------
studentLiftId studentId liftId weight
------------------------------------------------
1 1 1 185
2 2 3 130
3 3 1 190
4 1 2 120
5 2 1 155
6 3 2 145
7 1 1 135
8 1 1 205
9 2 3 200
10 1 3 150
11 2 2 110
12 3 3 250
I would like to have four top lists:
Bench Press
Parallel Squat
Power Clean
Total of the above 3
I can successfully grab a top list for each specific lift using the following query:
SELECT s.studentId, s.first, s.last, s.gender, s.weight, l.name, sl.weight
FROM Students s
LEFT JOIN (
SELECT *
FROM StudentLifts
ORDER BY weight DESC
) sl ON sl.studentId = s.studentId
LEFT JOIN Lifts l ON l.liftId = sl.liftId
WHERE l.name = 'Bench Press'
AND s.gender = 'm'
AND s.weight > 170
GROUP BY s.studentId
ORDER BY sl.weight DESC
However, I am stuck on how to add the highest total of each lift for each student. How can I first find the highest total for each student in each lift, and then add them up to get a total of all three lifts?
Edit
The result set that I am looking for would be something like:
-------------------------------------------------
studentId first last weight
-------------------------------------------------
3 John Doe3 585
1 John Doe 475
2 John Doe2 465
I also forgot to mention that I would actually like two lists, one for students above 170 and one for students below 170.
SELECT -- join student a total weight to the student table
A.studentId,
A.first,
A.last,
C.totalWeight
FROM
Student A,
(
SELECT -- for each studet add the max weights
sum(B.maxWeight) as totalWeight,
B.studentID
FROM (
SELECT -- for each (student,lift) select the max weight
max(weight) as maxWeight,
studentId,
liftID
FROM
StudentLifts
GROUP BY
studentId,
liftID
) B
GROUP BY
studentId
) C
WHERE
A.studentID = C.studentId
-- AND A.weight >= 170
-- AND A.weight < 170
-- pick one here to generate on of the two lists.