I have a coondition where I should show all employees under female managers. So I wrote this query.
select count(E.emp_id) as EmployeeCount, M.name, M.gender
from employee E Join employee M on M.emp_id = E.manager_id
where M.gender ='Female' group by M.name, M.gender
Above query returns
4 Camila Wolstenholme Female
7 Clemmy Lamberts Female
9 Janot Saxon Female
3 Tina Fain Female
But what I want is employees working under each female manager and sum of employees working under all female managers, i.e., 4+7+9+3=23.I want 23 to be shown along with employees working under each female manager. How can I do that
I believe that the following may do what you want :-
SELECT
(SELECT count() FROM employee ee JOIN employee mm ON mm.emp_id = ee.manager_id WHERE m.gender = 'Female' ) AS Total ,
count(E.emp_id) as EmployeeCount, M.name, M.gender,
group_concat(e.name)
FROM employee AS E
JOIN employee M on M.emp_id = E.manager_id
WHERE M.gender ='Female' group by M.name, M.gender
Example/Demo
DROP TABLE IF EXISTS employee;
CREATE TABLE IF NOT EXISTS employee (emp_id INTEGER PRIMARY KEY, name TEXT ,gender TEXT, manager_id INTEGER);
INSERT INTO employee (name,gender,manager_id) VALUES
('Camila Wolstenholme', 'Female',null),
('Clemmy Lamberts','Female',null),
('Janot Saxon','Female',null),
('Tina Fain','Female',null),
('Fred Bloggs','Male',1),
('Anne Smith', 'Female',1),
('Sarah Thompson','Female',1),
('Trudy Mann','Female',1),
('Jane X','Female',2),
('Audrey Wood','Female',2),
('Mary Bartman','Female',2),
('Teresa Owens','Female',2),
('Amanda Jones','Female',2),
('Sophie Alexander','Female',2),
('Andrea Turner','Female',2),
('Jessica Walsh','Female',3),
('Suzy Quertermain','Female',3),
('Alanah Taylor','Female',3),
('Catherine Wilkinson','Female',3),
('Rose Dennis','Female',3),
('Debbie Waterford','Female',3),
('Elaine Bywaters','Female',3),
('Farah Flannagan','Female',3),
('Gina Heartman','Female',3),
('Helen Inglis','Female',4),
('Ingrid Sachs','Female',4),
('Julie Zimmerman','Female',4),
('Frank Smith','Male',null) /* Male Manager */
;
SELECT
(SELECT count() FROM employee ee JOIN employee mm ON mm.emp_id = ee.manager_id WHERE m.gender = 'Female' ) AS Total ,
count(E.emp_id) as EmployeeCount, M.name, M.gender,
group_concat(e.name) AS Manages
FROM employee AS E
JOIN employee M on M.emp_id = E.manager_id
WHERE M.gender ='Female' group by M.name, M.gender
;
DROP TABLE IF EXISTS employee; /* Cleanup Environment */
Result
NULL has been used to indicate a Manager in the absence of an indication of what distinguishes a manager.
Additional
However showing total for each column could be confusing, is it possible to show it as a single row?
The following will show it as a single row with all other values. That is a) the total column is not extracted but instead a row is added via a UNION with the total in the first column and the other columns blanked out.
SELECT
/* (SELECT count() FROM employee ee JOIN employee mm ON mm.emp_id = ee.manager_id WHERE m.gender = 'Female' ) null AS Total ,*/
count(E.emp_id) as EmployeeCount, M.name, M.gender,
group_concat(e.name) AS Manages
FROM employee AS E
JOIN employee M on M.emp_id = E.manager_id
WHERE M.gender ='Female' group by M.name, M.gender
UNION SELECT (SELECT count() FROM employee ee JOIN employee mm ON mm.emp_id = ee.manager_id WHERE mm.gender = 'Female'),'','',''
;
Result
I have a table of ratings
|EMPLOYEE|Rating|
1 B
2 B
3 C
4 NULL
5 NULL
6 NULL
and i want to retrieve the count of the grades by each grading like so
Result set
|Rating|Count|
A 0
B 2
C 1
D 0
E 0
I used this query but the grades that isnt in the table will jsut appear as null
select rating,count(rating) from table group by rating
I also used this query which is basically a pivot of the above result set but for some reason it shows 3 rows of repeating data instead of just 1
select (select count(rating) from table where rating = 'E'),(select count(rating) from table where rating = 'D'),(select count(rating) from table where rating = 'C'),(select count(rating) from table where rating = 'B'),(select count(rating) from table where rating = 'A') from table group by rating
If you had a table for the assignable ratings then it would be quite simple (and flexible)
e.g. consider :-
Your existing table.
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (employee TEXT, rating text);
INSERT INTO mytable VALUES (1,'B'),(2,'B'),(3,'C'),(4,null),(5,null),(6,null);
The rating table.
DROP TABLE IF EXISTS rating;
CREATE TABLE IF NOT EXISTS rating (rating);
INSERT INTO rating VALUES('A'),('B'),('C'),('D'),('E');
Then :-
SELECT rating.rating, (SELECT count(*) FROM mytable WHERE rating.rating = mytable.rating) FROM rating;
Results in :-
Flexibility
Add some new ratings e.g. as per :-
INSERT INTO rating VALUES('X'),('Y'),('Z');
And then run:--
SELECT rating.rating, (SELECT count(*) FROM mytable WHERE rating.rating = mytable.rating) FROM rating;
results in :-
I would like to know if, and if yes, how I could accomplsh the following:
Lets say I have two tables:
Table A has two Columns: id, name
Table B columns: owner, argument
Now I am trying to find in table A all rows with specific name (animal) and use their ids to find it's argument value in table b. Those argument values are different ids in table a. So as a result I would like to get two columns. first has the id of the items who has the specific name (animal) I am looking for and second column has the name of the item which has the id that is argument of the initial ids.
table a (example)
id || name
1 || animal
2 || animal
3 || animal
4 || animal
15 || cat
16 || dog
17 || horse
18 || bird
...
table b (example)
owner || argument
1 || 15
2 || 16
3 || 17
4 || 18
...
result (example)
id || name
1 || cat
2 || dog
3 || horse
4 || bird
Thanks in advance for any hints / help.
Andreas
You need a double join from tablea to tableb and again doublea:
select
a.name ownwename,
t.name name
from tablea a
inner join tableb b
on b.owner = a.id
inner join tablea t
on t.id = b.argument
where a.name = 'animal'
See the demo
I believe the following will do what you want
SELECT owner, name FROM tableb JOIN tablea ON argument = id;
However, as using a subquery you could use :-
SELECT owner, (SELECT name FROM tablea WHERE argument = id) AS name FROM tableb;
Working Example :-
DROP TABLE If EXISTS tablea;
CREATE TABLE IF NOT EXISTS tablea (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO tablea (name) VALUES ('animal'),('animal'),('animal'),('animal'),('cat'),('dog'),('horse'),('bird'),
('animal'),('cat'),('dog'),('horse'),('bird'),('animal'),
('cat'),('dog'),('horse'),('bird') -- id's 15-18 inclusive
;
DROP TABLE IF EXISTS tableb;
CREATE TABLE IF NOT EXISTS tableb (owner INTEGER PRIMARY KEY, argument INTEGER);
INSERT INTO tableb (argument) VALUES(15),(16),(17),(18);
SELECT owner, name FROM tableb JOIN tablea ON argument = id;
SELECT owner, (SELECT name FROM tablea WHERE argument = id) AS name FROM tableb;
Results :-
and the second
I have a gridview and originally I was binding data from one table (Table 1) and it was straightforward
gvUsers.dataSource = class.getUsers()
gvUsers.databind()
Function getUsers () As List (Of Users)
Return (From X in Table1 Select x).ToList()
End Function
However, some of my data are pointing to another table and there is no relationship between tables ( no foreign key)
Table 1
UID Name Age Blood Type Test Type
1 Sam 22 2 3
2 Jane 23 2 4
Table 2
ID Domain Code Description
1 Blood Type A A
2 Blood Type B B
3 Test Type 1 1
4 Test Type 2 2
Currently In the gridView I see Blood Type as values 2 and Test type 3, 4 ( The second table ID) but I should get the Code column values in Table 2.
How can I join those two tables - I know if there is foreign key but the fact a column name is equivelant to row data name makes it hard for me to figure out!
Cheers
Try Code
var result = (from p in Table1.AsEnumerable()
join Blood in Table2.AsEnumerable() on p.BloodType equals Blood .ID
join Test in Table2.AsEnumerable() on p.TestTyoe equals Test .ID
select new With
{
uid = p.UID,
Name = p.name,
Age = p.age,
BloodType = Blood.Code,
TestType = Test .Code
}).ToList();
Sql Query IS :
select UID ,Name,Age,B.Code as BloodType ,T.Code as TestType From Table1
inner join Table2 B on Table1.BloodType = B.ID
inner join Table2 T on Table1.TestType= T.ID
The Used Vb then
Dim result = from p in Table1.AsEnumerable()
join Blood in Table2.AsEnumerable() on p.BloodType equals Blood .ID
join Test in Table2.AsEnumerable() on p.TestTyoe equals Test .ID
select
p.UID,
p.name,
p.age,
Blood.Code,
Test.Code
gvUsers.DataSource = result
Try this one:
select table1.*, table2.* from table1
inner join table2 on table1.Blood Type = table2.id
You can select you desired columns from both tables.
I am new to SQLite. I want to display the name of each employee and his/her manager's name. But I am a little confused about how to link the managerID with EmployeeID? Do I need to use subquery?
table: Employee
Columns: EmployeeID [pk], EmployeeName, ManagerID
Like:
EmployeeID EmployeeName ManagerID
1 Alice 2
2 Peter null
3 John 2
4 Mary 1
means Peter is Alice and John's manager and Alice is Mary's manager.
SELECT EmployeeName AS Employee, EmployeeName AS Manager
FROM Employee
WHERE
You need to join the table on itself with a LEFT OUTER join. Since the boss won't have a manager (null), use 'ifnull':
select A.EmployeeName as 'EmployeeName',
ifnull(B.EmployeeName, 'BOSS') as 'ManagerName'
from employee A left outer join employee B
on A.managerId = B.employeeId;