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
Related
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
emp_id project_id
3 2
3 3
7 1
7 2
7 3
7 4
3 27
7 32
3 31
8 2
8 3
9 2
9 3
Above is table of works on. How to fetch which employee is working on which project in mysql
Use GROUP_CONCAT:
SELECT emp_id, GROUP_CONCAT(project_id)
FROM mytable
GROUP BY emp_id
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.
I am trying make a complex query in MySQL i have following two tables
departments employees
Id parent title id name department status
--------------------------- ------------------------------------
1 0 Health 1 abc 3 1
2 0 Sports 2 def 3 1
3 0 Education 3 ghi 5 1
4 1 Physical 4 jkl 10 1
5 1 Mental 5 kkk 6 1
6 2 Football 6 lll 6 1
7 2 Baseball 7 sss 8 1
8 2 Beachball 8 xxx 6 1
9 2 Hockey 9 yyy 6 1
10 4 ENT 10 zzz 7 1
11 0 Finance 11 mnb 11 1
Departments table have four main departments(i-e: parent = 0) with multiple level depths of sub-departments.
Currently i have done it through a PHP function by running queries multiple time to achieve this, but still i want know if this is possible to fetch it with a Query.
Whats the best way OR how to select max 3 employee randomly for each main department with status 1.
The expected result should be something like
id name department maindep
1 abc 3 3
2 def 3 3
3 ghi 5 1
4 jkl 10 1
5 kkk 6 2
7 sss 8 2
10 zzz 7 2
11 mnb 11 11
i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/