How to resolve a category from a property table in SQL - sql-server-2008

By way of an example, lets say I have the following (simplified) table (called NumericValue):
Age Gender Occupation Location Value
40 M Accountant Johannesburg 100
40 F Accountant Johannesburg 120
40 M Engineer NULL 110
40 F Engineer NULL 110
Now suppose I have this table called Employees:
Employee Number Age Gender Occupation Location
1000123 40 F Engineer Cape Town
1000124 40 M Accountant Johannesburg
Now, what I need is to select the "value" field for these two employees. And let's say that engineers will never ever have a "location" in the NumericValue table, so I can't just do a simple join. In stead, I reformat my "NumericTable" as follows:
Table: "CategoryValue"
Category Value
1 100
2 120
3 110
4 110
With a "property" table like this:
Table: "CategoryProperty"
Category FieldName FieldValue
1 Age 40
1 Gender M
1 Occupation Accountant
1 Location Johannesburg
.
.
4 Age 40
4 Gender F
4 Occupation Engineer
(note, no entry for "location" under category 4, which refers to the 40 year old female engineer)
Which makes sense to me, since I only have entries where a specific categorisation field is of importance. But how do I resolve this and extract the Value field for the specific employee?
Thanks
Karl

Why don't you do something like this?
Select e.EmployeeNumber,
e.Age,
e.Gender,
e.Occupation,
e.Location,
nv.Value
From Employees e
Join NumericValue nv on e.Age = nv.Age
And e.Gender = nv.Gender
And e.Occupation = nv.Occupation
And e.Location = IsNull(nv.Location, e.Location)
Just replace the NumericValue Location with the value of the Employee Location when it is NULL

Related

SQL DATABASE SYSTEMS QUERY PROBLEM for Student database Universal Quantification

Relations:
Student - contains a sNumber and sName
sNumber
sName
10
Dave
20
Sam
30
Amy
40
Ron
50
Ava
Courses - contains the cNumber and cDescription
cNumber
cDescription
MATH100
Algebra 1
SCI200
Biology
ENG300
Writing 1
Enrollments - sNumber, cNumber, and final grade
sNumber
cNumber
final
10
MATH100
90
10
SCI200
85
20
ENG300
100
30
MATH100
99
30
SCI200
92
30
ENG300
91
50
ENG300
84
Universal Quantification Query: Find the student name and student number for the student(s) who have enrolled in ALL of the courses Dave has enrolled in. Dont include Dave in your answer.
The answer should be Amy and her sNumber.
My solution is not correct, Any help ???
Here is the solution i attempted in text form :
SELECT stud.sName, stud.sNumber
FROM Student stud
WHERE s.sName != 'Dave'
AND NOT EXISTS (
SELECT True
FROM Enrollment enroll
WHERE enroll.sNumber = stud.sNumber
AND NOT EXISTS (
SELECT True
FROM Courses course
WHERE course.cNumber = enroll.cNumber AND stud.sName = 'Dave'));

UNION ALL not working with different columns

Left join rows and right table rows in same column
I have two tables (Manager and Worker). The name of workers under a manager should come below manager name. The expected output is presented below:-
Manager Table:
ManagerCode Name Age Location
1 Chris 52 A
2 Rick 55 B
3 David 50 C
Worker Table
ManagerCode WName Age
1 Harry 33
1 Phil 40
2 Johnny 28
2 Jeff 47
Expected table:
ManagerCode Name Location
1 Chris A
1 Harry A
1 Phil A
2 Rick B
2 Johnny B
2 Jeff B
3 David C
Union All is creating problem for Location column as number of columns become different. I could use null as Location for worker table. But there are several columns like location in Manager. Is union correct option ?
You need to join the Worker and Manager tables to get the location codes for the workers from their corresponding managers. Then you can union that with the manager table.
SELECT ManagerCode, Name, Location
FROM (
SELECT ManagerCode, Name, Location, 1 AS isManager
FROM Manager
UNION ALL
SELECT w.ManagerCode, w.Name, m.Locationm, 0 AS isManager
FROM Worker AS w
JOIN Manager AS m ON w.ManagerCode = m.ManagerCode
) AS x
ORDER BY ManagerCode, isManager DESC
The isManager column is used to order the workers after their managers.

SQL query for allocation of managers fulfilling given criteria

I have a list of sellers and i have to allocated them managers. The data format is as below
Seller category Division Sales
David 17 A 5000
David 23 B 2000
David 91 C 100
David 25 C 200
Jack 23 A 4000
Jack 17 A 4000
Jack 25 B 3000
Now the list of managers is provided as below:
Manager preferred_category Designation
Tom 17 H
Aman 17 L
Sandy 23 H
John 23 L
Robin 91 H
Adam 91 L
Clara 25 H
Ted 25 L
Now the rules followed will be as below:
Sellers A and B division categories will be allotted to the desig 'H' manager of that category, and C category will get the the same from its A, B managers of the category with highest sales
If all division are C, then the seller will get only 1 manager, the L desig of the category with highest sales for the seller
I have managed to work it out in VBA but it is too much hassle in that and i want to automate in sql. I need to only find some kind of starting or way to start the process, i will be able to take that forward. Any help will be greatly appreciated.
Thanks in Advance
Here's a rough approach. Not guaranteed to work on any particular SQL flavour but I assume you have Access or SQL Server.
create two tables, SELLER and MANAGER and insert your data
on the SELLER table, add an Autonumber column called "seller_id", and a new integer column called "manager_id" (with no data yet)
on the MANAGER table, add an Autonumber column called "manager_id" as well
do some SQL to populate the SELLER.manager_id column:
Something like this for rule 1:
update seller set manager_id =
(select manager_id from MANAGER m
inner join SELLER s on s.category = m.preferred_category
where s.division in ('A','B') and m.designation = 'H'
and rownum < 2)
And for rule 2:
update seller set manager_id =
(select manager_id from MANAGER m
inner join SELLER s on s.category = m.preferred_category
where s.division = 'C' and m.designation = 'L'
and rownum < 2)
If you need to allocate more managers to sellers you'll need to create a cross-reference table and move the ID columns around

How to make a mysql query when a single table have the same value for two different attributes?

I have made two tables :
Member(member_id,name,contact)
Registration(loan_id,member_id,witness1_id,witness2_id,status)
primary key of Member,registration table are member_id and loan_id respectively.
The witness1_id and witness2_ id are the member_id in member table which means each member have two witnesses who are themselves the member of the organization.
I want to make a query which gives the list of members along with the witnesses details whose status is 1, output should be in the following schema :
(member_id,name_of_member,witness1_name,witness1_contact,witness2_name,
witness2_contact)
To make the problem easy to understand, I have not included the full schema rather I have used the required fields.
example :
Member
member_id name contact
1 xyz 1111111111
2 abc 2222222222
3 pqr 3333333333
4 efg 4444444444
Registration
loan_id member_id witness2_id witness2_id status
123 1 2 3 1
124 2 1 3 0
125 4 2 1 1
output/result
member_id name witness1_name witness1_contact witness2_name witness2_contact
1 xyz abc 22222222 pqr 333333333
4 efg abc 22222222 xyz 111111111
You need to prefix the columns with the name of the tables, or the aliases used in the query.
SELECT member.name, registration.status
FROM member
JOIN registration ON ( registration.member_id = member.member_id )
WHERE ...
This should do it:
SELECT r.member_id, m.name as name_of_member
, w1.name as witness1_name, w1.contact as witness1_contact
, w2.name as witness2_name, w2.contact as witness2_contact
FROM registration as r
LEFT JOIN member as m ON (r.member_id = m.member_id)
LEFT JOIN member as w1 ON (r.witness1_id = w1.member_id)
LEFT JOIN member as w2 ON (r.witness2_id = w2.member_id)
WHERE r.status = 1

MySQL - different description in table with ids from the same table

i have these tables:
tb_employee:
ID EMPLOYEE
1 Jhonatan Sandoval
2 Patricia Sanchez
3 Ken Dawson
tb_bankacc:
ID BANK AMOUNT OWNER (from tb_employee)
1 Bank 1 250000 1
tb_pay:
ID OWNER EMPLOYEE AMOUNT
1 1 2 500
2 1 3 480
I need to make a SELECT QUERY to show the names of employees, like this:
ID OWNER EMPLOYEE AMOUNT
1 Jhonatan Sandoval Patricia Sanchez 500
2 Jhonatan Sandoval Ken Dawson 480
But, i don't know how.
Use
SELECT p.Id, o.Employee AS Owner, e.Employee, p.Amount
FROM tb_pay p
INNER JOIN tb_employee e ON e.Id = p.Employee
INNER JOIN tb_employee o ON o.Id = p.Owner
You're joining to the tb_employee table twice, once to get the names for the Employee column (by joining the Id in the Employee column with the Id in tb_employee - and then taking the name from that record) and then again to get the names for the Owner column (joining the Id in the Owner column to the Id in tb_employee).