SQL Newbie Stuck on Manager Flag Field Logic - sql

This is my first month of being a Data Analyst, and I can't seem to find an answer that is specific enough to my problem here to help. I am having trouble getting this manager flag field to work and I think I'm getting confused by the joins.
The goal is to match the EMPLIDs of JOB_VW A to see if they exist in the Supervisor_ID column of Supervisor_VW K. Supervisor_VW K has ALL employees in the company (including supervisors) in the K.EMPLID column. Someone can be in both the supervisor ID column and EMPL column at the same time, but in different rows. The SUP ID is the EMPL ID of someone in a manager position.
For example:
Supervisor_VW K
EMPL ID EMPL NAME SUP ID SUP NAME
1 Smith, John 2 William, Mark
5 Jarvis, John 2 William, Mark
2 William, Mark 4 Rover, Spot
The results I am getting are as such
QUERY RESULTS
EMPLID EMPL NAME MANAGER FLAG
2 William, Mark Y
2 William, Mark Y
4 Rover, Spot Y
1 Smith, John N
5 Jarvis, John N
My current code is as follows:
SELECT CASE WHEN K.Supervisor_Id IS NULL THEN
'N'
ELSE
'Y'
END AS "ManagerFlag".....
FROM (SELECT K.*
FROM SUPERVISOR_VW K, JOB_VW A
WHERE K.SUPERVISOR_ID (+) = A.EMPLID
AND EXISTS (SELECT K1.EMPLID, K1.SUPERVISOR_ID
FROM SUPERVISOR_VW K1
WHERE K1.EMPLID IN K1.Supervisor_Id)
) K
So it seems that I am getting duplicate supervisor rows for every single employee that they supervise. If they supervise only one person, I get a singular row. If they supervise 20, I get 20 duplicate rows of that supervisor. HOWEVER, their employee that they supervise shows up in the table without issue and is properly labeled as N, no duplicates.
If anyone could help, please do! I appreciate you reading through my work, let me know if more info is required.

The usual way to achieve the desired result is like this:
select emplid, emplname,
case when emplid in (select supid from supervisor_vw) then 'Y'
else 'N' end as managerflag
from supervisor_vw;

Related

How to select people that don't know anyone who takes workshops

I have a few tables I want to iterate over. First table is Persons:
id
name
address
1
Laura Jansen
New York
2
Sana Vendi
Miami
3
Adam Smith
Boston
4
Mo Zora
Los Angeles
Second one is TakingWorkshop. This is the workshop the people are taking, so person_id is the id of the one in Persons.
id
person_id
workshop_id
20
4
26
19
2
27
18
3
28
Last table is Knows. The person id's are the same as the id's in Persons. So, PersonX knows PersonY.
PersonX
PersonY
1
2
1
3
2
1
4
1
So 1 is Laura, 2 is Sana and 3 is Adam. We can see that Adam doesn't know anyone. That means that Adam automatically also doesn't know anyone who takes workshops, because he doesn't even know anyone. However, in the table we see that Laura, 1, doesn't take workshops. So 4 and 2, Mo and Sana, know Laura, but she doesn't take any workshops so Mo and Sana don't know anyone who takes workshops.
I wrote some code for the people who don't know anyone taking workshops (in this database, it's Adam)
First I do a left join on the Person table and Knows table, on the id of persons and the id of personA of Knows. PersonA knows person B. This join gives me a table of people who know people, including the people who don't know anyone (those are null).
SELECT distinct P.name, K.personA_id
FROM Persons P LEFT JOIN Knows K
ON P.id = K.personA_id
Now I want to see if personB_id is in the person_id of TakingWorkshop. This way you can see whether the known people are taking workshops or not. PersonB_id should NOT be in TakeingWorkshop, because that's how you filter out Laura. I did this like this:
WHERE K.personB_id NOT IN (SELECT person_id
FROM TakingWorkshop)
So my whole code looks like this
SELECT distinct P.name, K.personA_id
FROM Persons P LEFT JOIN Knows K
ON P.id = K.personA_id
WHERE K.personB_id NOT IN (SELECT person_id
FROM TakingWorkshop)
But I get no results when I do this and want to know what's going wrong
Hmmm . . . Your description of the problem suggests not exists. But not exists what?
This query gets everyone who is known and taking a workshop:
select . . .
from knows k join
TakingWorkshop tw
on k.personY = tw.person_id;
So, we can slip that into the query:
select p.*.
from persons p
where not exists (select 1
from knows k join
TakingWorkshop tw
on k.personY = tw.person_id
where k.personX = p.id
);

MS Access: Selecting the first item according to a rank

Imagine I have a query called QueryA that returns stuff like this:
Employee Description Rank
John Happy 1
John Depressed 3
James Happy 1
James Confused 2
Mark Depressed 3
I am trying to make a query that grabs the Employee and the Description, but only one description -- the one with the best "rank." (the lower the rank the better). I sort QueryA by Employee then by Rank (descending).
So I'd want my new query QueryB to show that John as Happy, James as Happy, and Mark as Depressed.
However I try selecting Employee and then First of Description and it doesn't always work.
I'm not able to check this for Access, but it should work fine. Check my SQL Fiddle
select
r.employee, d.description
from
table1 as d
inner join (select min(rank) as rank, employee
from
table1
group by employee) r on d.rank = r.rank
and d.employee = r.employee

Group by a field not in select

I want to find how many modules a lecturer taught in a specific year and want to select name of the lecturer and the number of modules for that lecturer.
Problem is that because I am selecting Name, and I have to group it by name to make it work. But what if there are two lecturers with same name? Then sql will make them one and that would be wrong output.
So what I really want to do is select name but group by id, which sql is not allowing me to do. Is there a way around it?
Below are the tables:
Lecturer(lecturerID, lecturerName)
Teaches(lecturerID, moduleID, year)
This is my query so far:
SELECT l.lecturerName, COUNT(moduleID) AS NumOfModules
FROM Lecturer l , Teaches t
WHERE l.lecturerID = t.lecturerID
AND year = 2011
GROUP BY l.lecturerName --I want lectureID here, but it doesn't run if I do that
SELECT a.lecturerName, b.NumOfModules
FROM Lecturer a,(
SELECT l.lecturerID, COUNT(moduleID) AS NumOfModules
FROM Lecturer l , Teaches t
WHERE l.lecturerID = t.lecturerID
AND year = 2011
GROUP BY l.lecturerID) b
WHERE a.lecturerID = b.lecturerID
You should probably just group by lecturerID and include it in the select column list. Otherwise, you're going to end up with two rows containing the same name with no way to distinguish between them.
You raise the problem of "wrong output" when grouping just by name but "undecipherable output" is just as big a problem. In other words, your desired output (grouping by ID but giving name):
lecturerName Module
------------ ------
Bob Smith 1
Bob Smith 2
is no better than your erroneous output (grouping by, and giving, name):
lecturerName Module
------------ ------
Bob Smith 3
since, while you now know that one of the lecturers taught two modules and the other taught one, you have no idea which is which.
The better output (grouping by ID and displaying both ID and name) would be:
lecturerId lecturerName Module
---------- ------------ ------
314159 Bob Smith 1
271828 Bob Smith 2
And, yes, I'm aware this doesn't answer your specific request but sometimes the right answer to "How do I do XYZZY?" is "Don't do XYZZY, it's a bad idea for these reasons ...".
Things like writing operating systems in COBOL, accounting packages in assembler, or anything in Pascal come to mind instantly :-)
You could subquery your count statement.
SELECT lecturername,
(SELECT Count(*)
FROM teaches t
WHERE t.lecturerid = l.lecturerid
AND t.year = 2011) AS NumOfModules
FROM lecturer l
Note there are other ways of doing this. If you also wanted to elimiate the rows with no modules you can then try.
SELECT *
FROM (SELECT lecturername,
(SELECT Count(*)
FROM teaches t
WHERE t.lecturerid = l.lecturerid
AND t.year = 2011) AS NumOfModules
FROM lecturer l) AS temp
WHERE temp.numofmodules > 0

Trying to find duplication in records where address is different only in the one field and only by a certain number

I have a table of listings that has NAP fields and I wanted to find duplication within it - specifically where everything is the same except the house number (within 2 or 3 digits).
My table looks something like this:
Name Housenumber Streetname Streettype City State Zip
1 36 Smith St Norwalk CT 6851
2 38 Smith St Norwalk CT 6851
3 1 Kennedy Ave Campbell CA 95008
4 4 Kennedy Ave Campbell CA 95008
I was wondering how to set up a qry to find records like these.
I've tried a few things but can't figure out how to do it - any help would be appreciated.
Thanks
Are you looking to find something that shows the amount of these rows you have like this?
SELECT
StreenName,
City,
State,
Zip,
COUNT(*)
FROM YourTable
group by StreenName, City, State, Zip
HAVING COUNT(*) >1
Or maybe trying to find all of the rows that have the same street, city, state, and zip?
SELECT
A.HouseNumber,
A.StreetName,
A.City,
A.State,
A.Zip
FROM YourTable as A
INNER JOIN YourTable as B
ON A.StreetName = B.StreetName
AND A.City = B.City
AND A.State = B.State
AND A.Zip = B.Zip
AND A.HouseNumber <> B.HouseNumber
Here is one way to do it. You'll need a unique ID for the table to run this, as you wouldn't want to select the exact same person if theyre the only one there. This'll just spit out all the results where there is at least one duplicate.
Edit: Woops, just realized in comments it says varchar for the street number...hmm. So you could just run a cast on it. The OP never said anything about house numbers in varchar or being letters and numbers in the original post. As for letters in the street number field, I've been a third party shipping provider for 2 yrs in the past and I have never seen one; with the exception of an apt., which would be a diff field. Its just as likely that someone put varchar there for some other reason(leading 0's), or for no reason. Of oourse there could be, but no way of knowing whats in the field without response from OP. To run cast to int its the same except this for each instance: Cast(mt.HouseNumber as int)
select *
from MyTable mt
where exists (select 1
from MyTable mt2
where mt.name = mt2.name
and mt.street = mt2.street
and mt.state = mt2.state
and mt.city = mt2.city
and mt2.HouseNumber between (mt.HouseNumber -3) and (mt.HouseNumber +3)
and mt.UID != mt2.UID
)
order by mt.state, mt.city, mt.street
;
Not sure how to run the -3 +3 if there are letters involed...unless you know excatly where they are and you can just simply cut them out then cast.

Easiest way to find IsManager in SQL

Simple structure table of employees
Employee Manager
Joe Smith Jon Smith
Jon Smith Pete Stevens
Pete Stevens NULL
Jared Scho Pete Stevens
....
Im just trying to return some results but I want an indicator on whether the person is a manager or not so the result should be:
Employee Manager IsAManager
Joe Smith Jon Smith 0
Jon Smith Pete Stevens 1
Pete Stevens NULL 1
Jared Scho Pete Stevens 0
The result set is showing that Joe Smith and Jared Scho are not managers...
So If I had a simple SQL Query
SELECT
Employee,
Manager,
As IsAManager --tried to do a case statement here....
FROM
Employee
I tried to do a case statement something to this effect:
SELECT CASE ISNULL(COUNT(*), 0) > 0 THEN 1 ELSE 0 END FROM Employee WHERE Manager = Employee
Not sure how to word it :)
Hopefully this is just a demo example not your real table structure.
SELECT Employee,
Manager,
CASE
WHEN EXISTS(SELECT *
FROM Employee e2
WHERE e2.Manager = e1.Employee) THEN 1
ELSE 0
END As IsAManager
FROM Employee e1
For details of how SQL Server processes EXISTS Subqueries in CASE Expressions see this article.
To determine whether an employee is a manager, you need to match the Employee's ID (in this case, the name) against the list of Manager IDs (in this case the Manager column). If you find a match, the employee is a manager. If you don't find a match, the employee is not a manager.
You can do this with a LEFT OUTER JOIN as shown here:
SELECT DISTINCT
E.Employee,
E.Manager,
CASE WHEN M.Employee IS NULL THEN 0 ELSE 1 END As IsAManager
FROM
Employee E LEFT OUTER JOIN Employee M
ON E.Employee = M.Manager
Please note the following:
You did not specify the SQL product you're using, so I tried to make the solution general. I'm guessing from your attempt to use ISNULL that it's SQL Server, but this solution should work in any product that supports CASE.
Your method of storing manager status has one problem, which is that you cannot represent a manager with no employees (you derive the manager status from the existence of the employee-manager relationship). If you want to be able to store manager status separately from the employee-manager relationship then CREATE TABLE Managers (Employee. . . PRIMARY KEY). This will make the code necessary to get back manager status a little easier to write as well.