SQL Server 2008: creating a supervisor hierarchy in Cognos - sql-server-2008

I have a list of employees and with a list of immediate supervisors in the table. I want to create a select statement (I am limited because I am not using SQL Server directly just it as an engine) that not only produces the immediate supervisor but a supervisors supervisor in the next column. I have titles but I want to create one that is not limited by titles and or levels of management. Even just suggestions to try would be helpful.
This is how I started (the were clause is just so I can test data). I plan on flipping it too a two column select statement. I am not sure
SELECT PayeeID, tblLevel1.Sup1, Sup2
From (SELECT PayeeID ,SupervisorID AS Sup1
FROM PayeeHRAttribute
Where AsOfDate ='10/31/2015') AS tblLevel1 Right Join (Select PayeeID as Sup1, SupervisorID AS Sup2
FROM PayeeHRAttribute
Where AsOfDate = '10/31/2015') AS tblLevel2 On tblLevel1.Sup1 = tblLevel2.Sup1

Create 2 queries in Report Studio (look into Queries tab) and join them.
First table
supervisor
employee
Second table
supervisor as supersupervisor
employee as supervisor
Join it by supervisor field.
Create list with fields
employee
supervisor
supersupervisor

Related

Simple SQL 2 Tables data grouping

I need to complete the following task in SQL:
Show the employment status in individual offices of the company in the following layout: office code, country, number of employees.
Table employees:
Table offices:
My idea was:
SELECT officeCode, (SELECT country FROM offices), COUNT(*)
FROM employees
GROUP BY officeCode
But it seems it doesn't work... Can you help?
Your main data set would be the office. Join to the employee's table by the office code. Then from your main table, get the data you need and then do a count( employee number)
Remember to add all the fields that you are displaying to the group by.

Access - Only take row from table of max year

I have a table in Access that holds students previous qualifications. It has multiple lines per students dependent on the number of previous qualifications they have. The data has the year of the qualification in it.
I want to create a sub-query that only has the most recent qualification in it (i.e. the latest year). I have tried max and last in the query for the year, but because I am bringing out other fields, it is still pulling out all the qualifications rather than just the latest one.
StudentID Qualificationlevel QualificationType MaxOfYearAwarded
10203 Postgraduate/Masters MSc 2016
10203 Undergraduate BSc 2013
So in the example above, I want to ONLY have the top row being pulled as it is 2016 and therefore later than 2013.
Any help would be appreciated.
Quite a simple task
Just use a subquery as a condition.
To group the query, use a WHERE condition in the subquery. Note that, since we're querying the same table twice, we will need to use table aliases
SELECT *
FROM MyTable t
WHERE t.YearAwarded = (
SELECT Max(s.YearAwarded)
FROM MyTable s
WHERE s.StudentID = t.StudentID
)

Complex mysql update based on two fields and 'most' criteria?

update employee as a
set a.sup_role=(
select b.job_role from employee as b
where b.supervisorid=a.employeeid
group by b.job_role
order by count(b.job_role) desc
limit 1
)
where a.job_role='MAN1';
OK I have a table of all of our employees - around 100K.
All of our users have job roles that I built based on their employee.job_title. So based on 4K+ job_titles I knock that down to about 40 job_roles. This is so we can assign things in our CMS based on employee.job_roles. This has been working good but the problem is with the managers. The managers get some generic corporate job_title that we role into a generic job_role.
What you see above is code that I use to kind of do what I need to do - find out what job_role a supervisor based on what "most of their employees do". This outputs the correct sup_role but there are several things wrong with my code:
based on my syntax it won't let me update the employee table directly. I have have to update an employee "helper" table and fill in later.
So it is parsing through for the job_role "MAN1". First I don't want to update this to add new manager job roles. Also this doesn't account for my company doing ad hoc things like a manager just have a regular job title or different NULL fields.
And then the last part is that this code is taking 6 minutes to go through. Luckily I run this as a batch job but I am afraid it might cause crashes.
So I have the following table -
employee
with applicable fields
employeeid
supervisorid
job_title
job_role
sup_role
So below is my last attempt. It just runs and never outputs anything. I am wondering if I need to create a helper table to grab the DISTINCT supervisorid's since one employee could be many people's supervisor.
update employee as a
set a.sup_role=(
select b.job_role from employee as b
where b.supervisorid=a.employeeid
group by b.job_role
order by count(b.job_role) desc
limit 1
)
WHERE a.uid IN (select DISTINCT employee.supervisorid
from employee
where employee.supervisorid is not null
);

MySQL 2 foreign keys to the same primary key

So I have 2 tables; a task table and an employee table.
A task is created by an employee and maintains the id of the employee that creates it as creator_id.
A task is assigned to an employee and maintains the id of the employee it is assigned to as responsible_id.
The creator of the task and the employee it is assigned to can be different people.
How do I write a select statement that allows me to display the full names of both the employee that created the task and the employee that is assigned to it.
I think it might look something like this:
SELECT Task.Description, Employee1.name, Employee2.name
FROM Task, Employee Employee1, Employee Employee2
WHERE Task.creator_id = Employee1.id
AND Task.responsible_id = Employee2.id;
I have tried variations of this but it either returns errors or hits the memory limit.
Where am I going wrong?
From your description the query is correct but you should consider rewriting it to use explicit ANSI joins:
SELECT Task.Description, Employee1.name, Employee2.name
FROM Task
JOIN Employee Employee1 ON Task.creator_id = Employee1.id
JOIN Employee Employee2 ON Task.responsible_id = Employee2.id;
Sample SQL Fiddle

Use PowerPivot table as parameter in another SQL Server Query

I have information from 2 different SQL Server 2008 databases.
DB1 has the following related tables with Employee information
Employee
empDepartment
empLocation
DB2 has a table Refunds with refund requests.
Since they are on different Databases, I'm planning on using PowerPivot to create cross DB pivot tables to be able to show things like Refund requests by department, etc.
I'm able to bring in all the data with no issues EXCEPT for the Employee table. I don't have access to the source data. I'm pulling from a data warehouse of sorts, and there happen to be duplicate employee IDs in the data (great!). I've tried SELECT DISTINCT queries but the entire row isn't duplicated, in most cases it's just a bad Employee ID.
The other issue, is that only a small portion of employees (1-2K) submit refund requests on behalf of customers, so it seems silly to pull in the entire employee table (200k records).
So I got the crazy idea that I could dynamically pull the Employee table using the employeeID column in the Refunds table. But I can't seem to find a place to use existing PowerPivot tables in a separate PowerPivot query.
Here is the SQL for how I'm currently pulling the employee information
SELECT DISTINCT
emp.NTLogin as 'Employee_NTLogin'
, emp.LastName + ', ' + emp.FirstName as Emp_FullName
, emp.PositionCode as 'Position'
, emp.DepartmentID as 'Department'
, emp.LocationID as 'Location'
, emp.HireDate as 'Employee_HireDate'
, emp.EndDate as 'Employee_EndDate'
FROM dbo.Employee emp
WHERE
emp.EndDate IS NULL
OR emp.EndDate BETWEEN '04/01/2014 00:00:00' AND GETDATE()
ORDER BY
emp.LastName + ', ' + emp.FirstName
(the WHERE clause is filtering out employees that were current employees from 4/1 to today)
Not a lot of info about the actual pivot table you are using but here is an example of how to grab the data, you'll have to edit to match your tables.
ActiveSheet.PivotTables(1).PivotFields("Employee").PivotItems("ID").DataRange.Select
You'll probably want to place that data into an array and then pass that to your sql query. Not a complete answer, but hopefully it will get you on the path.