I have three table look like
1.Table Branch
BranchID = FK
Name
Address
Contact
2.Table Staff
Staff_ID = FK
Name
Address
Gender
Contact
Supervisor_ID = if does not have a supervisor then NULL
BranchID
3.Table Manager
Staff_ID = FK
Name
Address
Gender
Contact
BranchID
I would like to produce a listing showing the staff distribution at each branch. Include the branch number, manager name, total number of supervisors, and total number of male and female staff for each branch.
How do I retrieve a result set as above using an sql statement?
Below is my query
select Manager.Name,Branch.BranchNum,count(case when Supervisor_ID is null then 1 else null end) as NumberofSupervisor,count(case when Staff.gender='Male' and Supervisor_ID is not null then 1 else null end ) as Male, count(case when Staff.gender='Female' and Supervisor_ID is not null then 1 else null end)as female from Branch join Manager on Branch.BranchNum=Manager.BranchNum join Staff on Branch.BranchNum=Staff.BranchNum group by Branch.BranchNum
I get this error Column 'Manager.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Could you please guide me?
Thank you.
Related
I need to build a query to copy information in a column from one table to a column in another table.
This is how the tables looks like:
People:
PersonId
Name
StatusId
1
John
2
Jenny
3
Steve
Assignments:
AssignmentId
Country
PersonId
1
UK.
1
2
USA
3
Status:
StateId
Name
1
Busy
2
Free
There is a relationsihp between the People and Assignments tables: PersonId on the Assignments table is a FK. The People table has a relationship with the Status table through the FK StatusId. What I need to do is populate the StatusId on the table People with the StatusId from the table Status if the person in the table People exists on the table Assignments.
On the sample above both John and Steve are in the Assignments table, in this case theirs StatusId on the table People should be set to 1.
I was trying to do it with this:
update People
set StatusId = 1
where PersonId IN (
select PersonId
from Assignments
where Assignments.PersonId = People.PersonId
)
but as you can see I am hardcoding the StatusId what will not works. Is there some way to get the StatusId based on the result of the select? Or is there another way to get the StatusId?
If you want to refer to it by "name", you can use a subquery:
update People
set StatusId = (select s.StatusId from status s where name = 'Busy')
where PersonId IN (select a.PersonId from Assignments a where a.PersonId = People.PersonId);
I have a table groups
group_id | name_group
1 ISI
2 IZI
And a table students
id | first_name | last_name | group_id
6 Bob Surname1 1
17 John Surname2 2
How can I delete all information from student table by using groups.name?
i.e. I need query which select all students with the same group_id which is equivalent to name.
group_id 1 = 'ISI'
group_id 2 = 'IZI'
And a query must delete exactly by name.
You can use this query
Delete from Students where group_id=(Select group_id from groups where name_group='ISI');
This all the records with the group_id of 1 (via group_name='ISi').
There are different ways. A simple one, could be selecting the Id of the group and deleting from there. Example:
DECLARE
#name as nvarchar(20) = 'myName'
-- we display the data just for check
SELECT s.*, g.group_id
FROM students s ON g.group_id = s.group_id
WHERE g.name_group = #name
--we look the group id and delete the matches with students
DELETE
FROM students
WHERE group_id in (SELECT group_id FROM groups WHERE name_group = #name)
PD: This basic approach could work on both: MySQL and MSSQL.
I would not be surprised if this is a duplicate, but I have not been able to find this variation on the theme.
I have a table with two columns: one indicates data type, and the other the corresponding value. I want to convert this using SELECT so that each enumerated "type" is its own column populated by the values.
Table PersonInfo
InfoID PersonID AttributeType AttributeValue
1 1 email bob#example.com
2 1 dept research
3 2 email judy#example.com
4 2 dept engineering
5 3 email frank#example.com
The SELECT result will be:
PersonID email dept
1 bob#example.com research
2 judy#example.com engineering
3 frank#example.com NULL
As you can see, the InfoID index is no longer needed.
In my case, I know the potential values in the AttributeType column, so I don't need it to be completely dynamic.
Here is what I tried:
SELECT DISTINCT PersonID,
CASE AttributeType WHEN 'email' THEN AttributeValue ELSE null END as email
CASE AttributeType WHEN 'dept' THEN AttributeValue ELSE null END as dept
I know why this is failing, but I'm not sure what to do to get it to work as I would like.
select
personid,
max(case when attributetype='email' then AttributeValue end) email,
max(case when attributetype='dept' then AttributeValue end) dept
from
table
group by personid
I have a table with the fields ID,department_code,sub_department_code,class_code,desc_text
The desc_text has the name of the department when the sub_department_code and class_code fields are null, the sub_department name when the department_code and sub_department_code are not null but class_code is null, and the name of the class when the class_code is not null.
My issue is I have another table that has the id from the table above, but I would like to select the fields with department_code,desc_text(of the department),sub_department_code,desc_text(of the sub_department), etc.
I tried a few methods, but I am unsure even how to search for this issue.
I have the following two tables:
Hierarchy
id,department_code,subdept_code,class_code
1 1 null null
2 1 1 null
3 1 1 1
4 2 1 null
Hierarchy_detail
id,hierarchy_id,short_text
1 1 car
2 2 truck
The hierarchy.id is linked to hierarchy_detail.hierarchy_id
With this, I have a table that has data I want, which the fk from hierarchy.id, which I cna link and get the department, sub-sept and class codes, but I am having trouble getting the text on there for each type.
You want to use a case statement to capture the logic. If Im' reading correctly, it looks something like this:
select id, department_code, sub_department_code, class_code,
(case when sub_department_code is null and class_code is null
then desc_text
end) as department_name,
(case when department_code is not null and sub_department_code is not null and class_code is null
then desc_text
end) as sub_department_name,
(case when class_code is not null then desc_text
end) as class_name
from t
You can then use this query as a subquery to join to other tables.
I have the following database design:
Employee Table: EmployeeID, Name, OrgCode
Department Table: OrgCode, DepartName
CompleteSurvey Table: ID, RespondentID
Now, since I have some employees in the Employee Table with Null Values in the OrgCode column, I want to replace the NULL values with 'Others' value since I have 'Others' in the Department Table with the (OrgCode = 4). So is there any query to do this for me?
Use this query:
UPDATE Employee SET OrgCode = 4 WHERE OrgCode IS NULL
This should work for ya as a one time update to a single database:
update employee
set OrgCode = 4
where OrgCode is null
However, in other databases, 'OrgCode' may not be number 4, so here's an update that should work for all databases in your organization (or in case 'OrgCode' is not number 4).
update employee
set OrgCode = d.OrgCode
from departments d
where d.DepartmentName = 'Others'
and employee.OrgCode is null
Also, you might want to check the DBMS you're using to see if you can set a default value on the table's column. This way, whenever an insert occurs and the OrgCode is missing, 'Others' will be filled in automatically.
well engines are different, but the idea is always the same:
update employee set OrgCode = 'Others' where OrgCode IS NULL
I would try this:
UPDATE employee
SET orgcode = NVL( orgcode, 4 );