SUM()查询提供错误的结果

时间:2014-05-11 16:35:39

标签: mysql sum

我的数据库中有三个表 - 员工,工资,班次。 班次表由staff_id链接到staff表,薪水由salary_band_id链接,也在员工表中。

我正在努力计算员工的工资(Shift.Total_Hours * Salary.Hourly_Wage)

这是我的问题:

SELECT Staff.First_Name, Staff.Last_Name, Shift.Staff_ID, Shift.Total_Hours, Sa.Hourly_Wage,
       SUM(Shift.Total_Hours * Sa.Hourly_Wage) Total
FROM Shift, Salary Sa, Staff
INNER JOIN Salary ON Staff.Salary_Band_ID = Salary.Salary_Band_ID
WHERE Shift.Staff_ID = Staff.Staff_ID
GROUP by Staff_ID
ORDER BY Total;

表:

CREATE TABLE Shift (
Staff_ID INT(5) NOT NULL,
Date DATE NOT NULL,
Time_Started TIME NOT NULL,
Time_Ended TIME NOT NULL,
Total_Hours DOUBLE(4,2) NOT NULL,
Confirmed ENUM('y','n') NOT NULL
)

INSERT INTO Shift
(Staff_ID, Date, Time_Started, Time_Ended, Total_Hours, Confirmed)
VALUES 
('1', '2012-06-08', '9:00', '16:45', '7.75', 'y'),
('3', '2012-06-18', '13:10', '17:30', '4.33', 'y'),
('6', '2012-10-14', '11:15', '16:45', '5.5', 'y'),
('4', '2012-10-30', '10:00', '17:00', '7', 'n'),
('5', '2013-07-15', '9:10', '17:20', '8.1', 'y'),
('10', '2013-08-10', '9:00', '17:00', '8', 'n'),
('8', '2013-10-05', '10:15', '17:30', '7.25', 'y'),
('7', '2013-11-06', '9:20', '17:00', '7.66', 'y'),
('2', '2014-03-25', '9:00', '16:45', '7.75', 'n'),
('9', '2014-04-11', '9:00', '17:00', '8', 'n'),
('11', '2014-06-05', '9:00', '17:00', '8', 'y');

CREATE TABLE Salary (
Salary_Band_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Position VARCHAR(40) NOT NULL,
Hourly_Wage DOUBLE(4,2) NOT NULL
)

INSERT INTO Salary
    (Position, Hourly_Wage)
    VALUES 
    ('Worker', '6'),
    ('Worker Manager', '8'),
    ('General Manager', '10.22'),
    ('Manager', '13.45'),
    ('Owner', '25'),
    ('Brewer', '7.76'),
    ('Shop Assistant', '6.12');

CREATE TABLE Staff (
Staff_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Salary_Band_ID INT(5) NOT NULL,
First_Name VARCHAR(25) NOT NULL,
Last_Name VARCHAR(25) NOT NULL,
Part_Full_Time ENUM('f','p') NOT NULL,
DOB DATE NOT NULL,
Start_Date DATE NOT NULL, 
End_Date DATE,
Address_ID INT(7) NOT NULL,
Contact_Number VARCHAR(12) NOT NULL,
Availability_ID INT(5) NOT NULL,
Brewery_ID INT(4),
Shop_ID INT(4)
)

INSERT INTO Staff
    (Salary_Band_ID, First_Name, Last_Name, Part_Full_Time, DOB, Start_Date, Address_ID, Contact_Number, Availability_ID, Brewery_ID, Shop_ID)
    VALUES 
    ('1', 'Jenny', 'Bolick', 'p', '1966-08-15', '2005-06-06', '1',  '+447700900969', '1', '1', '1'),
    ('2', 'Margit', 'Eves', 'f', '1968-01-13', '2005-10-06', '2',   '07700900711', '1', '1', '2'),
    ('3', 'Ramonita', 'Layton', 'f', '1975-11-28', '2005-09-29', '3',   '07700900336', '1', '1', '1'),
    ('4', 'Kattie', 'Speck', 'p', '1977-10-24', '2006-09-07', '6',  '07700900615', '1', '1', '2'),
    ('5', 'Christa', 'Denk', 'p', '1968-03-25', '2008-04-11', '5',  '07700900542', '2', '2', '4'),
    ('6', 'Francene', 'Scholl', 'p', '1991-08-05', '2009-05-06', '12',  '07700900763', '2', '1', '4'),
    ('7', 'Patti', 'Tomaszewski', 'f', '1974-01-22', '2010-03-11', '11',    '07700900125', '3', '2', '6'),
    ('7', 'Pandora', 'Laplant', 'p', '1982-02-14', '2011-10-03', '11',  '+447700900118', '2', '3', '5'),
    ('1', 'Branden', 'Bermejo', 'f', '1985-08-15', '2012-04-18', '10',  '07700900687', '4', '3', '6'),
    ('1', 'Lanell', 'Delao', 'p', '1987-12-24', '2012-04-20', '9',  '07700900057', '5', '3', '2'),
    ('1', 'Floria', 'Vandermark', 'f', '1991-03-13', '2013-11-13', '6',     '07700900396', '6', '4', '2');

结果一切都很好,除了Totals离开了!它似乎只选择一个salary_band ..£6.60 /小时任何人都可以看到我做错了吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的,使用显式JOIN而不是隐式。

SELECT 
st.First_Name, 
st.Last_Name, 
sh.Staff_ID, 
sh.Total_Hours, 
sa.Hourly_Wage,
SUM(sh.Total_Hours * sa.Hourly_Wage) Total
from Staff st
inner join Salary sa on sa.Salary_Band_ID = st.Salary_Band_ID
inner join Shift sh on sh.Staff_ID = st.Staff_ID
GROUP by st.Staff_ID
ORDER BY Total
desc
;

<强> DEMO