Top 5 Employees from each dept with lowest attendance - sql-server

How do I find top 5 employees from each department with lowest attendance?
---Employee Table
--- Department Table
--- Attendance Table
I tried in this way to find top 5 salary from each department but how to apply in this specially for attendance table with employee and department table how to find top 5 employees with lowest attendance please assist me in this regard with cte.
Thanks
With ctetemp As
(
Select E.EmpId, E.Ename, E.Salary,D.Dname,
DENSE_RANK() OVER(PARTITION BY D.DeptId ORDER BY Salary desc) as rnw
from EmployeeTbl AS E JOIN Departmet AS D
ON D.DeptId = e.Dept_Id_Emp
)
Select * from ctetemp
where rnw IN (5)

You will need to reference the attendance table.
I'm assuming 'a' means 'absent' in this context.
SELECT
AbsenceSummary.DeptID
,AbsenceSummary.EmpId
,AbsenceSummary.AbsentDays
,AbsenceSummary.AbsentRank
FROM
(
SELECT
dept.DeptId
,att.EmpId
,COUNT(*) AS AbsentDays
,DENSE_RANK() OVER (PARTITION BY dept.DeptId ORDER BY COUNT(*) DESC) AS AbsentRank
FROM
attendance att
INNER JOIN
employee emp
ON emp.EmpID = att.EmpId
INNER JOIN
department dept
ON dpt.DeptId = emp.DeptId
WHERE
dept.DeptId IN (<the departments you care about>)
AND att.atdate >= <your minimum date>
AND att.atdate <= <your maximum date>
AND att.attendance = 'a' --absent
GROUP BY
dept.DeptId
,att.EmpId
) AbsenceSummary
WHERE
AbsenceSummary.AbsentRank <= 5
For the record, the AtId column in this situation is useless and possibly counterproductive. The key is (EmpId,atDate).

Related

Write a query to display the name of the department that has the maximum staff count order by department name-this is the question what should i add?

select department.department_name from department
join(select department_id,count(staff_id) as staff_count from staff group by department_id)s on
department.department_id=s.department_id
order by department.department_name desc ;
It worked for me when I executed both the queries. You can try either of them.
select department_name
from
(
select d.department_name,
count(*) as staff_count
from department d
join staff s
on d.department_id=s.department_id
Group by d.department_name
Order by staff_count DESC
)
where rownum <= 1;
Or
select top 1 department_name
from staff
join department
on s.department_id = d.department_id
group by department_name
order by count(*) desc

Find the third-highest salary in each department

I am trying to find the third-highest salary in each department if there is such.
SELECT DepartmentID
FROM Employees
GROUP BY DepartmentID
This is what I can do.
I looked at similar posts but not sure I understand how to do it with my table.
You can use row_number function to assign a order of salary, then get the 3rd one:
SELECT s.DepartmentID, s.Salary
FROM (
SELECT DepartmentID, Salary, ROW_NUMBER() OVER(PARTITION BY DepartmentID ORDER BY Salary DESC) AS salary_rank
FROM Employees) s
WHERE s.salary_rank=3
SELECT e.first_name,
e.last_name,
d.department_name,
salary,
ROW_NUMBER() OVER ( PARTITION BY d.id ORDER BY salary DESC ) AS
salary_rank
FROM department d
JOIN employee e ON d.id = e.department_id
ORDER BY department_name;

SQL Server 2014 Consolidate Tables avoiding duplicates

I have 36 Sales tables each referred to one store:
st1.dbo.Sales
st2.dbo.Sales
...
st35.dbo.Sales
st36.dbo.Sales
Each record has the following key columns:
UserName, PostalCode, Location, Country, InvoiceAmount, ItemsCount, StoreID
Here is SQLFiddle
I need to copy into Customers table all Username (and their details) that are not already present into Customers
in case of duplicated it is required to use the fields of record where InvoiceAmount is MAX
I tried to build a query but looks too complicated and it is also wrong because in CROSS APPLY should consider the full list of Sales Tables
INSERT INTO Customers (.....)
SELECT distinct
d.UserName,
w.postalCode,
w.location,
W.country,
max(w.invoiceamount) invoiceamount,
max(w.itemscount) itemscount,
w.storeID
FROM
(SELECT * FROM st1.dbo.Sales
UNION
SELECT * FROM st2.dbo.Sales
UNION
...
SELECT * FROM st36.dbo.Sales) d
LEFT JOIN
G.dbo.Customers s ON d.Username = s.UserName
CROSS APPLY
(SELECT TOP (1) *
FROM s.dbo.[Sales]
WHERE d.Username=w.Username
ORDER BY InvoiceAmount DESC) w
WHERE
s.UserName IS NULL
AND d.username IS NOT NULL
GROUP BY
d.UserName, w.postalCode, w.location,
w.country, w.storeID
Can somebody please give some hints?
As a basic SQL query, I'd create a row_number in the inner subquery and then join to customers and then isolated the max invoice number for each customer not in the customer table.
INSERT INTO Customers (.....)
SELECT w.UserName,
w.postalCode,
w.location,
w.country,
w.invoiceamount,
w.itemscount,
w.storeID
FROM (select d.*,
row_number() over(partition by d.Username order by d.invoiceamount desc) rownumber
from (SELECT *
FROM st1.dbo.Sales
UNION
SELECT *
FROM st2.dbo.Sales
UNION
...
SELECT *
FROM st36.dbo.Sales
) d
LEFT JOIN G.dbo.Customers s
ON d.Username = s.UserName
WHERE s.UserName IS NULL
AND d.username IS NOT NULL
) w
where w.rownumber = 1
Using your fiddle this will select distinct usernames rows with max invoiceamount
with d as(
SELECT * FROM Sales
UNION
SELECT * FROM Sales2
)
select *
from ( select *,
rn = row_number() over(partition by Username order by invoiceamount desc)
from d) dd
where rn=1;
step 1 - use cte .
select username , invoiceamount ,itemscount from Sales
UNION all
select user name , invoiceamount ,itemscount from Sales
.....
...
step 2
next cte use group by and get max invoiceamount ,itemscount for user of last result set.
,cte2 as (
select user name , max (invoiceamount) as invoiceamount ,max(itemscount) as itemscount from cte)
step3
use left join with user table and find missing record and itemscount invoiceamount

SQL select top 10 for each year

I have a fact database from which I want to make a trendline based on top 10 items based on sum quantity for each item per year.
I've done the following, but it does for example select more than 10 entities for my year 2007:
select TOP 10 sum(Quantity) as Quantity,DIM_Time.Year, DIM_Item.Name as Name
from Fact_Purchase
join DIM_Item on DIM_Item.BKey_ItemId = Fact_Purchase.DIM_Item
join DIM_Time on DIM_Time.ID = Fact_Purchase.DIM_Time_DeliveryDate
where Fact_Purchase.DIM_Company = 2 and DIM_Time.ID = FACT_Purchase.DIM_Time_DeliveryDate
Group by dim_item.Name, DIM_Time.Year
Order by Quantity DESC
How do I select top 10 items with the highest quantity through all my years, with only 10 top entities for each year?
As you can guess, the company is individual, and Is going to be a parameter in my report
I think this is what you're going for. My apologies if I messed up on translating your tables across.
select *
from (
select DIM_Time.[Year], dim_item.Name, SUM(Quantity) Quantity, RANK() OVER (PARTITION BY DIM_Time.[Year] ORDER BY SUM(Quantity) DESC) salesrank
from Fact_Purchase
join DIM_Item on DIM_Item.BKey_ItemId = Fact_Purchase.DIM_Item
join DIM_Time on DIM_Time.ID = Fact_Purchase.DIM_Time_DeliveryDate
where Fact_Purchase.DIM_Company = 2 and DIM_Time.ID = FACT_Purchase.DIM_Time_DeliveryDate
group by dim_item.Name, DIM_Time.[Year]
) tbl
where salesrank <= 10
order by [Year], salesrank
The subquery groups by name/year, and the RANK() OVER part sets up a sort of row index that increments by SUM(Quantity) and restarts for each Year. From there you just have to filter out anything with a salesrank (index) that's over 10.
SELECT
_year,
Name,
_SUM,
RANK_iD
FROM
(
SELECT
_year,
Name,
_SUM,
DENSE_RANK()OVER(PARTITION BY _year,_Month ORDER BY _SUM DESC) AS RANK_iD
FROM(
Select
DIM_Time AS _year,
DIM_Item as Name,
sum(Quantity) AS _SUM
from
#ABC
GROUP BY
_year,
Name
)A
)B
WHERE RANK_iD<=10

T-Sql query for the situation - Need a departmentID with max number of employees?

let's say employee table has employee details and deptId of the employee.
to get the number of employees in each deptid,
select deptId, COUNT(*) from employee group by deptId;
question is: to get the deptId having max number of employees of the above result set,
select Top 1 deptId, COUNT(*) from employee group by deptId order by 2 desc
(2-ref to second column in the query list) - will do.. but
Is there anyway to avoid ordering this set? or better way of writing this sql,
thanks
If you just want the MAX number of employees within a department, you can do this:
SELECT TOP 1 DepartmentID,
COUNT(EmployeeID)
FROM EmployeeTable
GROUP BY DepartmentID
ORDER BY COUNT(EmployeeID) DESC
Without any ordering, it is hard, but try
Select deptId, cnt
From (Select deptId, count(*) cnt
from employee
Group By deptId) Z
Where cnt = (Select Max(cnt)
From (Select deptId, count(*) cnt
From employee
Group By deptId) ZZ)

Resources