2014-05-11 142 views
0

我在我的數據庫中有三張表 - 員工,工資,班次。 換班表由staff_id鏈接到員工表,薪資由員工表中的salary_band_id鏈接。SUM()查詢給出錯誤結果

我試圖計算做(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'); 

結果所有看起來都不錯,除了總計不在路上!此外,它似乎只選擇一個薪水帶..£6.60 /小時任何人都可以看到我做錯了什麼?

enter image description here

+0

你能提供一些數據與http://sqlfiddle.com表和預期結果的問題一起? –

+0

它不會讓我這樣做,因爲其他表我會把這些放在很大的過度複雜它不一定,但我已經插入數據到問題希望有所幫助 – user1875797

+0

好吧,讓我檢查 –

回答

1

我認爲這是你在找什麼,用明確的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

+0

哇,這是完善!感謝您的時間和專業知識,非常感謝! – user1875797

+1

它爲你工作很好。主要的問題是你有'離職,薪水,工作人員',然後加入薪水意味着你再次加入薪水和工資,從而得到意想不到的結果。 –