SQL Server - 需要查询工资大于平均工资且低于每个办公室经理的工资

时间:2018-03-30 10:18:09

标签: sql sql-server

我有两张桌子......

CREATE TABLE Branch
(
    branchID int,
    b_location varchar(50),
    b_contactNo int,

    CONSTRAINT PK_Branch PRIMARY KEY (branchID)
);   

CREATE TABLE Staff
(
    staffID int,
    fullName varchar(100) NOT NULL,
    s_category varchar(25),
    s_email varchar(50),
    s_contactNo int,
    speciality varchar(100),
    qualifications varchar(250),
    pre_employment varchar(200),
    salary numeric(8,2),
    staff_gender char(1),
    staff_joined_date datetime,
    branch_allocated int,

    CONSTRAINT PK_Staff PRIMARY KEY (staffID),

    CONSTRAINT FK_Staff_Branch 
        FOREIGN KEY (branch_allocated) REFERENCES Branch(branchID) ON DELETE CASCADE,

    CONSTRAINT CHK_StaffGender CHECK (staff_gender='M' OR staff_gender='F'),
    CONSTRAINT CHK_FullName CHECK (fullName NOT LIKE '%[^A-Za-z ]%'),
    CONSTRAINT CHK_SALARY CHECK (salary>0 AND salary<=150000)
);

员工表有这个样本数据:

 7 | Nick Carter   | Nurse          | nick@mail.com   | 32145657 | N/A | N/A | 5 Years in London General |  99000.00 | M | 2013-01-04 10:00:00.000 | 1
 8 | Brian OConner | Nurse          | brian@mail.com  | 32678579 | N/A | N/A | 3 Years in London General |  83000.00 | M | 2015-01-09 10:00:00.000 | 2
12 | Jayden Smith  | Office Manager | jayden@mail.com |  5678755 | N/A | N/A | N/A                       | 145000.00 | M | 2013-01-04 10:00:00.000 | 1
13 | Will Smith    | Office Manager | will@mail.com   | 12345678 | N/A | N/A | N/A                       | 135000.00 | M | 2013-01-04 10:00:00.000 | 2

我想知道每个分支机构的员工薪水高于平均薪水,而不是每个分支机构的办公室经理

我写过一些问题。但不确定如何继续。

SELECT 
    s.staffId AS ID, s.fullName AS Name, s.s_category AS Category, s.salary AS Salary, b.branchId AS 'Branch No'
FROM 
    Staff s 
INNER JOIN 
    Branch b ON s.branch_allocated = b.branchID
WHERE 
    salary > (SELECT AVG(salary) FROM Staff)

WITH tempTable AS 
(
    SELECT DISTINCT branch_allocated, AVG(salary) AS AVG_SAL 
    FROM Staff
    GROUP BY branch_allocated
)
SELECT AVG_SAL 
FROM tempTable

3 个答案:

答案 0 :(得分:2)

更改您的WHERE查询

WHERE 
    salary > ( SELECT AVG(salary) 
               FROM   Staff x 
               WHERE  x.branch_allocated = s.branch_allocated )
AND
    Salary < ( SELECT MIN(salary) 
               FROM   Staff x 
               WHERE  x.s_category = 'Office Manager' ) 

答案 1 :(得分:1)

SELECT s.staffId AS ID, 
       s.fullName AS Name, 
       s.s_category AS Category, 
       s.salary AS Salary, 
       b.branchId AS 'Branch No'
FROM Staff s 
INNER JOIN Branch b 
ON s.branch_allocated = b.branchID
WHERE salary > ( SELECT AVG(salary) 
           FROM   Staff x 
           WHERE  x.branch_allocated = s.branch_allocated )
AND salary < (SELECT MIN(salary) FROM Staff  
          where speciality ='Office Manager' Group by branch_allocated)

答案 2 :(得分:0)

with empavgsal as(
SELECT s.staffId AS ID, s.fullName AS Name, s.s_category AS Category, s.salary AS Salary, b.branchId AS 'BranchNo' FROM Staff s INNER JOIN Branch b ON s.branch_allocated = b.branchID WHERE s.salary > (SELECT AVG(salary) from Staff Group By s_category where s_category!='Office Manager') 
)
Select e.ID,e.Name,e. Category,e.Salary,e.BranchNo FROM empavgsal INNER JOIN Staff s ON
s.staffId!=e.ID WHERE e.salary<s.s salary AND s.s_category ='Office Manager'