i'm using sql server 2008 rs1 and I have a problem with a query .
my table is named : employees
table columns are :
first name , last name , job title , gender , date of birth and salary .
The required : to display job title, employee name, and the difference between salary of the employee and minimum salary for the job
I try to solve it as this :
SELECT First_Name, Title ,Salary-min(Salary) AS (Differance) FROM Employees;
but this cause an error
help me please
thanks in advance
Assuming that minimum salary is the minimum for all the employees you need to select the minimum from all employees and use;
SELECT
First_Name,
Title,
Salary - (SELECT MIN(SALARY) FROM Employees) As Difference
FROM Employees
try this.
;WITH cte
AS (SELECT Min(Salary) Differance
FROM Employees)
SELECT First_Name,
Title,
Salary - (SELECT * FROM cte) AS Differance
FROM Employees;
You can use a sub-query to calculate the min-salary:
SELECT First_Name, Title ,Salary-(SELECT MIN(e2.Salary)
FROM Employees e2) AS Difference
FROM Employees
You don't need to be afraid that this is calculated for every row.
try this
;WITH cte(minimumSalray)
AS
(
SELECT MIN(Salary)
FROM Employees
)
SELECT First_Name, Title ,Salary-cte.minimumSalray AS Differance
FROM Employees,minimumSalary
Don't have a SQL Server 2008 R2, just tried on SQL Server 2014.
SELECT First_Name, Title ,
Salary-min(Salary) OVER() AS [Differance]
FROM Employees;
If you want to get minimum salary of the same job, just query
SELECT First_Name, Title ,
Salary-min(Salary) OVER(PARTITION BY Title) AS [Differance]
FROM Employees;
Related
I want to find the total amount of associates at a company, as well as how many female and male engineers all in one query.
I am able to just get the sum of all associates on one row when that is the only thing that my query is looking for, but as soon as I try and combine it with the query looking for the number of males an females, the associate job titles start to separate.
My current code looks like this:
SELECT
count(*) as [Number of Employees], Gender, Job
FROM
#table
WHERE
Job like '%Associate%'
GROUP BY grouping sets
((Job), (Gender))
the result sets have a row for each type of associate job, and I am trying to figure out how to combine them under one row under the name 'associate'
Here is the simple way
SELECT SUM(numberofemployees) AS total,
Gender,
CASE WHEN JobTitle LIKE '%Engineer%' THEN 'Engineer' ELSE JobTitle END AS JobTitle
FROM #table
GROUP BY Gender,
CASE WHEN JobTitle LIKE '%Engineer%' THEN 'Engineer' ELSE JobTitle END
OUTPUT:
total Gender JobTitle
5 F NULL
3 M NULL
8 NULL Engineer
Check this script. In addition, this will also return "Engineer" value in Job Title column for F & M. But if it is your requirement to have NULL there, some adjustment required in the script.
Note: Sub query is just for a better understanding. This can be also achieved in a single query.
SELECT COUNT(*),
GENDER,
JobTitle
FROM
(
SELECT
JobTitle,
Gender,
CASE
WHEN JobTitle like '%Engineer%' THEN 'Engineer'
ELSE JobTitle
END AS JobTitle
FROM HumanResources.EmployeeFROM
)A
WHERE A.JobTitle = 'Engineer'
GROUP BY GENDER,JobTitle
Add this:
SELECT
count(*) as [Number of Employees],
Gender,
JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle like '%Engineer%'
Group by grouping sets
((JobTitle), (Gender))
HAVING
COUNT(*)>=3
Ok so for what you want
You can try this because
SELECT SUM(a.[Number of Employees]) AS Value1, a.Gender , a.JobGroup FROM (
SELECT 'Engineer' AS JobGroup, COUNT(*) AS [Number of Employees], Gender, JobTitle FROM HumanResources.Employee WHERE JobTitle LIKE '%Engineer%' GROUP BY grouping sets ((JobTitle), (Gender))) a
GROUP BY a.Gender,a.JobGroup
i have a table name as testtabl1 now i want to find all the student who are enroll in 2 or more courses,
in this sid is the student id and cid is the course id
table structure
create table testtabl1(Sid int,cid int,years varchar(20))
insert into testtabl1(Sid,cid,years)
select 1,1,'2016'
union all
select 2,2,'2017'
union all
select 1,2,'2017'
new to sql server and stackoverflow need help !!
tried
select sid,COUNT(*),cid from testtabl1 group by sid,cid having count(*)>1
SELECT sid as StudentId,
COUNT(cid) as SelectedCoursesCount
FROM testtabl1
GROUP BY sid
HAVING COUNT(cid) > 1;
i have a db2 linked server i'm running a query on through SQL Server.
select *
from openquery (DO,'
select distinct HOUSE_NUM, NAME, DOB, AGE, row_number()
over(partition by DOB) rownum
from schema.INFO
where HOUSE_NUM = ''332''
group by HOUSE_NUM, NAME, DOB, AGE
order by NAME, rownum desc
limit 1
with ur');
the table has historical records, so there is a row for each age of the individual. i want to select the highest numbered row for each partition because that will give me their current age, however when i put limit 1 to select the top result i only get 1 row which ignores all the other people. the problem is there are multiple people living in a house and i need all of their ages, not just one of them. how do i select the top result of each partition in db2?
Before applying limit
After applying limit, i need the other names too
A Db2 query would look like this - rownumber cannot be referred to in the same query part this is why I used a CTE
with temp as (
select distinct HOUSE_NUM, NAME, DOB, AGE
, row_number() over(partition by HOUSE_NUM, NAME, DOB order by age desc) as rownum
from schema.INFO
where HOUSE_NUM = '332'
)
select *
from temp
where rownum = 1
Hope this helps - due to the limited information about the data it is only a best guess
I have three tables in SQL Server:
Pay
Allowances
Deductions
All of them have following columns: empID, Amount.
I am writing a query to select amounts from all three tables using IN. Following is my query
Select sum(P.Amount), sum(A.Amount), sum(D.Amount)
from Pay P, Allowances A, Deductions D
where P.empID=A.empID=D.empID IN (Select EmpId from Employees)
Basically I want to get Pay, Allowance and Deductions of each employee one by one. But I cannot get the query correct.
It looks like you need an UNION ALL query:
SELECT empID, 'Pay' AS fromtable, Amount
FROM Pay
WHERE empID IN (1200,1201)
UNION ALL
SELECT empID, 'Allowances' AS fromtable, Amount
FROM Allowances
WHERE empID IN (1200,1201)
UNION ALL
SELECT empID, 'Deductions' AS fromtable, Amount
FROM Deductions
WHERE empID IN (1200,1201)
How do I get this statement (which returns duplicates):
Select Name from Table
To return this:
Select Distinct Name, Count(Number of non-distinct Name rows)
?
Thanks in advance.
SELECT Name, COUNT(Name) AS DistinctCount
FROM tableName
GROUP BY Name
Try:
select
Name,
count(Name)
from tableName
GROUP BY name