Aggregate one table twice on different level - sql

I am a little bit a newbie in SQL and am struggling with a seemingly easy task.
Let's see the data:
FirstName LastName ID DepartmentNumber ManagerID
Aliana Abramova 1111111111 4 4610226861
Boriana Borova 2222222222 4 4610226861
Cali Moldovanska 3333333333 4 4610226861
Anelia Simeonova 4009016246 1 4009016246
Maria Tacheva 4206174562 3 4206174562
This is an employee table. What I am trying to do is to extract these employees that are managers (ID = ManagerID) but only these out of them that work in a department that have more than one employees (so only these that have a count of ID grouped by DepartmentNumber >0)
I can do this tasks separately:
Select FirstName, LastName, ID
from Employee
where ID = ManagerID;
Select count(ID)
from Employee
group by DepartmentNumber;
It is hard for me though, to somehow merge this knowledge into one query and combined the data so that I know which are these IDs that belong to employees that are both managers and in their department work more than 1 person.
I have done similar tasks but when it comes to merging 1-2-3 tables grouped on different levels (and merged by different keys) I get somehow confused. Probably I need to make an interim select statement but now sure how.

You can use EXISTS:
SELECT *
FROM dbo.YourTable A
WHERE EXISTS(SELECT 1 FROM dbo.YourTable
WHERE DepartmentNumber = A.DepartmentNumber
GROUP BY DepartmentNumber
HAVING COUNT(*) > 1)
AND ID = ManagerID;

Related

How to use Max while taking other values from another column?

I am new in SQL and have problem picking the biggest value of a column for every manager_id and also other information in the same row.
Let me show the example - consider this table:
name
manager_id
sales
John
1
100
David
1
80
Selena
2
26
Leo
1
120
Frank
2
97
Sara
2
105
and the result I am expecting would be like this:
name
manager_id
top_sales
Leo
1
120
Sara
2
105
I tried using Max but the problem is that I must group it with manager_id and not being able to take name of the salesPerson.
select manager_id, max(sales) as top_sales
from table
group by manager_id ;
This is just an example and the actual query is very long and I am taking the information from different tables. I know that I can use the same table and join it again but the problem is as I mentioned this query is very long as I am extracting info from different tables with multiple conditions. And I don't want to make a temporary table to save it. It should be done in one single query and I actually did solve this but the query is super long due to the inner join that I used and made original table twice.
My question is that can I use Max and have the value in the name column or is there other method to solve this?
Appreciate all help
You can use row_number() with CTE to get the highest sales for each manager as below:
with MaxSales as (
select name, manager_id, sales,row_number() over (partition by manager_id order by sales desc) rownumber from table
)
select name , manager_id ,sales from MaxSales where rownumber=1

How to replace a column value for the matching records within a table in Oracle

I have a table which has some matching values of employees i.e. an employee could be in multiple departments.
I wanted to identify those records on the basis of their "name" and "dob". if it is same then replace the "id" as an increment of the decimal.
In below example: Mike is in 2 departments (IT, Finance) so I want his IT dept id (as an increment of the decimal) in the final outcome. Base id can identified on the basis department IT.
Please let me know how can I do this?
Let's take the min id and the row number divided by 10:
SELECT
MIN(id) OVER(PARTITION BY name, dob) + ((ROW_NUMBER() OVER(PARTITION BY name, dob ORDER BY id)-1)/10 as id,
name,
dob,
department
FROM
emp
I chose the min id for the employee as the base id. If you have a different strategy, like you want IT dept id to form the base value, instead of MIN(id) consider something like FIRST_VALUE(id) OVER(PARTITION BY ... ORDER BY case when department = 'it' then 0 else 1 end)
I agree with tim though; there seems a good deal of your question thatvis unclear poorly specified or not completely thought through. What if an emp is in 10 departments and an id conflict occurs? Generally we don't care about what an id number is so we don't change it or try to fill up gaps etc

Excessive Case Statement Help - SQL Server

I'm supposed to answer this for class, and it's tricky (for me)
Write a SELECT query to output the name of all employees with the name of their supervisor. If the employee has no supervisor, the supervisor name column should contain the text 'No Supervisor'.
The primary key field in my db is the employeeid and they are provided with names, and each student also has a supervisorid
The table for this is shown below (sorry for the layout):
employeeid lastname firstname salary supervisorid
1 Stolz Ted 25000 NULL
2 Boswell Nancy 23000 1
3 Hargett Vincent 22000 1
4 Weekley Kevin 22000 3
5 Metts Geraldine 22000 2
6 McBride Jeffrey 21000 2
7 Xiong Jay 20000 3
I was wondering how I could go about this statement without using the case statement to apply each of the 7 students with:
when concat(firstname,' ',lastname)='Nancy Boswell' then 'Ted Stolz'
In larger tables this would simply be a HUGE statement, is there a better way to do it?
Thanks!
EDIT:
I've now tried this:
SELECT
EMP1.employeeid as 'employee',
EMP2.supervisorid as 'manager'
FROM
employee EMP1
LEFT OUTER JOIN
employee EMP2
ON
emp1.employeeid = emp2.supervisorid;
However, I am seeing duplicate fields, for some reason employee 2 and 3 are appearing twice, meaning there are 9 fields showing instead of 7.
Also, I need to display their names, not their id's does that mean I need to join the join that i've already done to the employee name ? How would I do this?
Thanks for the feedback guys!
You need to link the table with itself based on the supervisorId. This might be strange if you are new to SQL but it is very common to do. You tell with SQL to add the row of the supervisor to the row of the employee via its primary key.
SELECT
*
FROM
EMPLOYEES EMP1
LEFT OUTER JOIN
EMPLOYEES EMP2
ON
-- make link between tables here
Note that the above query is not 100% correct / complete, its an indication. The LEFT OUTER JOIN statement makes the employees without supervisor have null values for the supervisor, otherwise the whole record would be left out.

Display all managers and employees without a manager

I have a table in SQL Server 2008 that for explanation purposes contains, ID, Employee and ManagerID.
eg:
ID Employee ManagerID
1 A NULL
2 B 2
3 C 2
I want to write a query that returns all non related ManagerID's and ID's where ManagerID is equal to the ID.
The result should look like this,
ID Employee ManagerID
1 A NULL
2 B 2
In essence no managers can be managers of managers.
At first I thought that it would be simple using a SELF Join and an EXCLUDE SQL statement however I cannot get this to work. I would prefer not to USE the EXCLUDE statement as my actual table has more columns and related data that I would like to return.
If you could help, I would be grateful.
select employee, managerid
from your_table
where managerid is null
or managerid = id

UPDATE query that fixes orphaned records

I have an Access database that has two tables that are related by PK/FK. Unfortunately, the database tables have allowed for duplicate/redundant records and has made the database a bit screwy. I am trying to figure out a SQL statement that will fix the problem.
To better explain the problem and goal, I have created example tables to use as reference:
alt text http://img38.imageshack.us/img38/9243/514201074110am.png
You'll notice there are two tables, a Student table and a TestScore table where StudentID is the PK/FK.
The Student table contains duplicate records for students John, Sally, Tommy, and Suzy. In other words the John's with StudentID's 1 and 5 are the same person, Sally 2 and 6 are the same person, and so on.
The TestScore table relates test scores with a student.
Ignoring how/why the Student table allowed duplicates, etc - The goal I'm trying to accomplish is to update the TestScore table so that it replaces the StudentID's that have been disabled with the corresponding enabled StudentID. So, all StudentID's = 1 (John) will be updated to 5; all StudentID's = 2 (Sally) will be updated to 6, and so on. Here's the resultant TestScore table that I'm shooting for (Notice there is no longer any reference to the disabled StudentID's 1-4):
alt text http://img163.imageshack.us/img163/1954/514201091121am.png
Can you think of a query (compatible with MS Access's JET Engine) that can accomplish this goal? Or, maybe, you can offer some tips/perspectives that will point me in the right direction.
Thanks.
The only way to do this is through a series of queries and temporary tables.
First, I would create the following Make Table query that you would use to create a mapping of the bad StudentID to correct StudentID.
Select S1.StudentId As NewStudentId, S2.StudentId As OldStudentId
Into zzStudentMap
From Student As S1
Inner Join Student As S2
On S2.Name = S1.Name
Where S1.Disabled = False
And S2.StudentId <> S1.StudentId
And S2.Disabled = True
Next, you would use that temporary table to update the TestScore table with the correct StudentID.
Update TestScore
Inner Join zzStudentMap
On zzStudentMap.OldStudentId = TestScore.StudentId
Set StudentId = zzStudentMap.NewStudentId
The most common technique to identify duplicates in a table is to group by the fields that represent duplicate records:
ID FIRST_NAME LAST_NAME
1 Brian Smith
3 George Smith
25 Brian Smith
In this case we want to remove one of the Brian Smith Records, or in your case, update the ID field so they both have the value of 25 or 1 (completely arbitrary which one to use).
SELECT min(id)
FROM example
GROUP BY first_name, last_name
Using min on ID will return:
ID FIRST_NAME LAST_NAME
1 Brian Smith
3 George Smith
If you use max you would get
ID FIRST_NAME LAST_NAME
25 Brian Smith
3 George Smith
I usually use this technique to delete the duplicates, not update them:
DELETE FROM example
WHERE ID NOT IN (SELECT MAX (ID)
FROM example
GROUP BY first_name, last_name)