I am getting employee data from source which gives me 2 rows for same employee. 1st row has a salary and 2nd has commission. to identify if it is salary or commission I have got one Flag column.
Now I want to store it in single row in my target table, where I would have salary and commision as columns.
try a chance maybe work
select employee_id,sum(salary)salary,sum(commission) from
(select employee_id,0 as salary,commission from tblname where flag=1
union all
select employee_id ,salary ,0 commission from tblname where flag=0
)a
group by employee_id
Use conditional aggregation
select employee_id,max(case when flag=0 then salary end) as salary,
max(case when flag=1 then commission end) as commission
from tablename
group by employee_id
Related
Write a SQL query to get the second highest salary from the Employee table.
| Id | Salary |
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
| SecondHighestSalary |
| 200 |
This is a question from Leetcode, for which I entered the following code:
SELECT CASE WHEN Salary = ''
THEN NULL
ELSE Salary
END AS SecondHighestSalary
FROM (SELECT TOP 2 Salary
,ROW_NUMBER() OVER (ORDER BY Salary DESC) AS Num
FROM Employee
ORDER BY Salary DESC) AS T
WHERE T.Num = 2
It says that the query does not return NULL if there's no value for second highest salary.
For eg. if the table is
| Id | Salary|
| 1 | 100 |
The query should return
|SecondHighestSalary|
| null |
and not
|SecondHighestSalary|
| |
Solution to the Leetcode 2nd highest salary problem is:
Select
Max(Salary) AS SecondHighestSalary
from Employee
where Salary < (
Select Max(Salary) from Employee
);
In case of ties you want the second highest distinct value. E.g. for values 100, 200, 300, 300, you want 200.
So get the highest value (MAX(salary) => 300) and then get the highest value less than that:
select max(salary) from mytable where salary < (select max(salary) from mytable);
Using window functions, utilizing NTH_VALUE gives a clean answer
SELECT (
SELECT NTH_VALUE(Salary, 2) OVER(ORDER BY Salary DESC) AS SecondHighestSalary
FROM Employee
GROUP BY Salary
LIMIT 1 OFFSET 1 ) AS SecondHighestSalary
;
Detailed Break-Down:
The outer SELECT Statement is required to get NULL value incase a value was not found (second rank is not present for example, or table only has one row)
NTH_VALUE(Salary, 2) is basically saying, look at each group (in our case it divides the table based on groups of Salary) and for each group add a column that lists the second highest value, for every row within the same group from that new column, we want to pick the second most paid (so only second row)
NTH_VALUE() OVER(ORDER BY) is in ASC order by default, make sure you explicit the DESC order
NTH_VALUE() merely gives the order to the rows within each group (here Salary) incase of two similar salaries in the same salary group it will give them separate ranks (ex 1 and 2) even if they have same value in the same group, For this use GROUP BY () statement
Because NTH_VALUE() merely gives the order to the columns, based on a group USE LIMIT 1 to get just one value (top value) and OFFSET 1 (to make that top value our targeted second most paid)
you should be able to do that with OFFSET 1/FETCH 1:
https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx
SELECT id, MAX(salary) AS salary
FROM employee
WHERE salary IN
(SELECT salary FROM employee MINUS SELECT MAX(salary)
FROM employee);
You can try above code to find 2nd maximum salary.
The above code uses MINUS operator.
For further reference use the below links
https://www.techonthenet.com/sql/minus.php
https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/
You can use RANK() function to rank the values for Salary column, along with a CASE statement for returning NULL.
SELECT
CASE WHEN MAX(SalaryRank) = 1 THEN NULL ELSE Salary as SecondHighestSalary
FROM
(
SELECT *, RANK()OVER(ORDER BY Salary DESC) As SalaryRank
FROM Employee
) AS Tab
WHERE SalaryRank = 2
It would be better to use the DENSE_RANK() function so that ranks don't get skipped whenever there is a tie for a position.
I would use DENSE_RANK() & do LEFT JOIN with employee table :
SELECT t.Seq, e.*
FROM ( VALUES (2)
) t (Seq) LEFT JOIN
(SELECT e.*,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS Num
FROM Employee e
) e
ON e.Num = t.Seq;
While you can use a CTE (from MSSQL 2005 or newer) or ROWNUMBER the easiest and more "portable" way is to just order by twice using a subquery.
select top 1 x.* from
(select top 2 t1.* from dbo.Employee t1 order by t1.Salary) as x
order by x.Salary desc
The requisite to show null when there's not a second bigger salary is a bit more tricky but also easy to do with a if.
if (select count(*) from dbo.Employee) > 1
begin
select top 1 x.* from
(select top 2 emp.* from dbo.Employee emp order by emp.Salary) as x
order by x.Salary desc
end
else begin
select null as Id, null as Salary
end
Obs:. OP don't said what to do when the second largest is a tie with the first but using this solution is a simple matter of using a DISTINCT in the IF subquery.
Here is the easy way to do this
SELECT MAX(Salary) FROM table WHERE Salary NOT IN (SELECT MAX(Salary) FROM table);
You can try this for getting n-th highest salary, where n = 1,2,3....(int)
SELECT TOP 1 salary FROM (
SELECT TOP n salary
FROM employees
ORDER BY salary DESC) AS emp
ORDER BY salary ASC
Hope this will help you. Below is one of the implementation.
create table #salary (salary int)
insert into #salary values (100), (200), (300)
SELECT TOP 1 salary FROM (
SELECT TOP 2 salary
FROM #salary
ORDER BY salary DESC) AS emp
ORDER BY salary ASC
drop table #salary
The output is here 200 as 300 is first highest, 200 is second highest and 100 is the third highest as shown below
salary
200
Here n is 2
Query:
CREATE TABLE a
([Id] int, [Salary] int)
;
INSERT INTO a
([Id], [Salary])
VALUES
(1, 100),
(2, 200),
(3, 300)
;
GO
SELECT Salary as SecondHighestSalary
FROM a
ORDER BY Salary
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY
| SecondHighestSalary |
| ------------------: |
| 200 |
select case
when cnt>1 then SecondHighestSalary
else null end as SecondHighestSalary
from
(select top 1 Salary as SecondHighestSalary,
(select count(distinct Salary) from Employee) as cnt
from (
select distinct top 2 Salary
from Employee
order by Salary desc ) as sal
order by SecondHighestSalary asc) as b
Select salary from employees limit 1,1 ;
Very easy way to find second highest salary
select
case when max(salary) is null then null else max(salary) end SecondHighestSalary
from (
select salary , dense_rank() over (order by salary desc) as rn
from Employee
)r
where rn = 2
This code returns null when there is no second highest salary
You can use the union condition to handle the null case
SELECT Salary as "SecondHighestSalary" from Employee
WHERE Salary < (SELECT MAX(salary) FROM Employee )
UNION
(SELECT null)
ORDER BY 1 DESC
LIMIT 1;
You can use exists() together with If-else statements.
Query:
If exists(select distinct salary as SecondHighestSalary from employee
order by salary desc offset 1 row fetch first 1 row only)
select distinct salary as SecondHighestSalary from employee
order by salary desc offset 1 row fetch first 1 row only
else select null as SecondHighestSalary;
#Please check the below code#
SELECT TOP 1 secondhighestsalary
FROM (SELECT
CASE WHEN z.SalaryRank >1 THEN Salary
ELSE null END secondhighestsalary, SalaryRank
FROM
(SELECT salary, RANK() OVER(ORDER BY Salary DESC) As SalaryRank
FROM Employee GROUP BY salary )z
GROUP BY Salary, SalaryRank
)c
ORDER BY secondhighestsalary DESC
Explanation:
** SELECT salary, RANK() OVER(ORDER BY Salary DESC) As SalaryRank
FROM Employee GROUP BY salary**
above query is to provide rank to Salary column, group by clause will take care the duplicate values ..... next
**SELECT
CASE WHEN z.SalaryRank >1 THEN Salary
ELSE null END secondhighestsalary, SalaryRank
FROM
(SELECT salary, RANK() OVER(ORDER BY Salary DESC) As SalaryRank
FROM Employee GROUP BY salary )z **
next piece of code assign NULL against rank 1 and salary for greater than 1 rank.
So if you have only one row in the table and as per our question we need to display second highest salary if not display NULL, it will take care that situation.
At last we need to Order by DESC and take the Top 1 record.
SELECT max(Salary) as SecondHighestSalary from Employee where salary <>(SELECT max(salary) from Employee)
So, this is the exercise:
Make a query that displays the number of presidents (job_id AD_PRES), the total salary sum of the presidents, the number of administration vice presidents (job_id AD_VP), and the total salary sum of those vice presidents.
The way it was shown to me:
Select count(decode(job_id,'AD_PRES',1,0)) AS NumOfPres,
Sum(decode(job_id,'AD_PRES',salary,0) AS SumSalaryP,
count(decode(job_id,'AD_VP',1,0)) AS NumOfPres,
Sum(decode(job_id,'AD_VP',salary,0) AS SumSalaryVP
FROM EMPLOYEES;
This is what I did:
Select count (e.job_id),sum(e.salary),count(m.job_id),sum(m.salary)
FROM employees e join employees m on(e.job_id=m.job_id)
Where e.job_id='AD_PRES'
AND m.job_id='AD_VP';
So, join is more readable I would say but is there any other performance difference?
Your join is forcing the relationship between the employees. If there is no such relationship then all results would return 0.
Also you are linking all employees that are president with all employees that are vice-president. So if you have 3 presidents and 10 vicepresidents, the join will result in 30 rows and the sum will be repeated to calculate the total, something that would never happen in the first query.
The exercise didn't specify return a single row, so the simple way is:
Select
job_id,
count(*),
sum(salary)
FROM EMPLOYEES
where job_id in ('AD_PRES', 'AD_VP')
group by job_id
The 1st Select uses legacy syntax and will not return a correct result as count(decode(job_id,'AD_PRES',1,0)) is the same as count(*).
And it's reading all rows, there should be a WHERE:
Select
sum(case when job_id = 'AD_PRES' then 1 else 0 end) AS NumOfPres,
Sum(case when job_id = 'AD_PRES' then salary else 0 end) AS SumSalaryP,
sum(case when job_id = 'AD_VP' then 1 else 0 end) AS AS NumOfVPs,
Sum(case when job_id = 'AD_PRES' then salary else 0 end) AS SumSalaryVP
FROM EMPLOYEES
where job_id in ('AD_PRES', 'AD_VP');
Your 2nd Select doesn't work at all. You join on matching job_ids, but one is 'AD_PRES' and the other 'AD_VP', so no match.
When you want to join it would be a CROSS JOIN:
Select *
from
( Select
count(*) AS NumOfPres,
sum(salary) AS SumSalaryP
FROM EMPLOYEES
where job_id ='AD_PRES'
)
cross join
( Select
count(*) AS NumOfVPs,
sum(salary) AS SumSalaryVP
FROM EMPLOYEES
where job_id ='AD_VP'
)
I am trying to find the number of employees in a table that earn exactly the maximum salary of all the employees in the table called tblPerson.
Select Max(x.[No of Employees]) as Number, x.Salary as Salary
from
(
Select Count(Id) as [No of Employees], Salary
from tblPerson
Group by Salary
Having Salary = MAX(Salary)
)x
where x.[No of Employees]=3
Now I know this is a kind of long and complex way of doing it, but I was trying to do it using a derived table. But I am getting the error:
"Column 'x.Salary' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
My question is, why am I getting this particular error since the main query is a simple Select statement with a where clause. Isn't it??
Mainly, aggregate functions work only with other aggregate functions or grouped by columns.
Why? Because an aggregate function needs to know the set of values to do calculation with.
In you case, the max() will want to use all the data available for the calculation and display a single result (single row) and the other column will want to be displayed row by row. So there's a conflict.
Thank you all. Every answer helped me. However, I think I found a pretty simple way to do it:
Select top 1 Count(Id) as [No of Employees], salary
from tblPerson
Group by Salary
Order by [No of Employees] DESC
select count(*) from tblPerson where salary=(select max(salary) from tblPerson)
You get the error because 'max' is an aggregation, while you have nothing to aggregate the number by.
Select Max(x.[No of Employees]) as Number, x.Salary as Salary
from
(
Select Count(Id) as [No of Employees], Salary
from tblPerson
Group by Salary
Having Salary = MAX(Salary)
)x
---------
Group by Salary -- all other items in your select statement
---------
where x.[No of Employees]=3
however, you can also use a temporary table or variable to find the persons.
To solve this via a variable, you could do the following
declare #maxSalary Decimal
set #maxSalary = (Select max(salary) from tblperson) --insert the max value into a variable
Then either aggregate the persons (or do some other logic):
Select ID from tblperson where salary = #maxSalary
The reason for not using a group by is that using a variable is more efficient, as you search the table instead of aggregating over it.
Create a CTE (RESULT), and using DENSE_RANK function, get the highest salary, together with the EmployeeID's.
The first row of the RESULT table will give the highest salary.
Using the aggregate function COUNT, get the number of Employees with the highest Salary.
with RESULT (EmployeeID, Salary, DenseRank) as
(select EmployeeID, Salary,
DENSE_RANK() over (ORDER BY SALARY DESC) AS DenseRank
from Employee)
select TOP 1 Salary,
(select COUNT(EmployeeID)
from Employee
where Salary = (select TOP 1 Salary)
from RESULT
where DenseRank = 1)
)
from RESULT
where DenseRank = 1;
I have a sql table called Employee in which I have EmployeeID, Status and DepartmentID columns. Now I have been assigned with the task of creating a tabular report where in I need count of total employees for a department, count of Employees which are active(Status), and DepartmentID.
Getting the Count of total employees and corresponding DepartmentID is simple with a Group By clause.
Select count(*) as 'Total Employees',DepartmentID from Employees
Group By DepartmentID
How would I get the Count of Employees with a particular Status for the same DepartmentID as the outer query?
Please let me know if you want some more clarifications about the problem.
If you have column Status with values active , no need of outer query
Select count(*) as 'Total Employees',
SUM(CASE WHEN status='Active' THEN 1 ELSE 0 END ) as TotalActiveEmployees,
DepartmentID
from Employees
Group By DepartmentID
Try this one.
Select count(*) as 'Total Employees',
COUNT(CASE WHEN status='Active' THEN 1 ELSE 0 END ) as TotalActiveEmployees,
DepartmentID
from Employees
Group By DepartmentID
I have a table named employee_salary, with three columns: empsal_id, empsal_name and empsal_sal. The data is as follows:
empsal_id empsal_name empsal_sal
1 dilip 14000
2 santosh 20000
3 amit 32000
4 dilip 22000
5 amit 38000
6 santosh 25000
7 dilip 30000
The empsal_id is an Identity column with a Seed and an Increment of 1, and is the Primary Key. I want to return the name and current salary of each employee. Salary can decrease as well as increase, so current does not necessarily mean highest.
So I need the following output:
empname emp_sal
dilip 30000
amit 38000
santosh 25000
I am using Microsoft SQL Server, and I have to do this in a single query.
This query will return each employee, along with their highest salary:
SELECT
empsal_name, MAX(empsal_sal)
FROM
employee
GROUP BY
empsal_name
This query will return each employee, along with their current salary (i.e. the salary with the highest empsal_id:
SELECT
empsal_name, empsal_sal
FROM
employee e1
WHERE
empsal_id =
(SELECT MAX(empsal_id)
FROM employee e2
WHERE e1.empsal_name=e2.empsal_name)
Personally, I think you would be better off using an effective date column (e.g. empsal_effectivedate) to determine which record is the most current, so this query will return each employee, along with their current salary (i.e. the salary with the most recent empsal_effectivedate), assuming there is an empsal_effectivedate field:
SELECT
empsal_name, empsal_sal
FROM
employee e1
WHERE
empsal_effectivedate =
(SELECT MAX(empsal_effectivedate)
FROM employee e2
WHERE e1.empsal_name=e2.empsal_name)
Assuming there is also an ID in the table and also assuming that the larger this ID is the most recent the salary is for that employee you can do
SELECT DISTINCT
e.empname,
(SELECT TOP 1
emp_sal
FROM
employee s
WHERE
s.empname = e.empname
ORDER BY
recid DESC) AS emp_sal
FROM
employee e
(also assuming that empname is unique for an employee)
because of the many assumptions though : you should probably post all the columns of the table and what they mean ..
SELECT empname, MAX(emp_sal)
FROM employee
GROUP BY empname
UPDATED:
SELECT DISTINCT EmpA.empname,
EmpA.emp_sal
FROM Employee AS EmpA
INNER JOIN ( SELECT EmpName, MAX(recID) AS recid
FROM Employee
GROUP BY EmpName
) AS EmpB ON EmpA.recid = EmpB.recid;
You need a date field to determine the latest inserted row . In case if the table is linked to some other table which has date column in it .Then its pretty easy to fetch the current data .
For Example
Employee Table
{
EmpName varchar(30) PK,
EmpAddress varchar(255) ,
Company varchar(30),
CurrentTimeStamp Datetime
}
Salary Table
{
EmpName varchar(30) FK,
EmpSalary int
}
To get the Latest record use the CTE function
With LatestSal(EmpName ,EmpSalary)
AS
(
Select row_number() over (PARTITION BY b.[EmpName], order by CurrentTimestamp DESC) as seq
b.EmpName,b.EmpSalary
From Employee as a,
Salary as b
on a.[EmpName]=b.[EmpName]
)
Select EmpName,EmpSalary
from LatestSal
where seq=1