简体   繁体   中英

Calculate SUM from one table and display columns from another

I have 2 tables, Employee, and Transaction

Transaction

tID cID carID eID tDate PickupDate ReturnDate Amount_Due

Employee

eID fName lName Job Manager Hired

I need to calculate the commission (2.5%) and display that along with fName and lName.

I've calculated the commission, I think I've done the join correctly but can't quite figure out how to add in a SELECT to show the other two columns.

SELECT t.Amount_Due
    , SUM(t.Amount_Due*0.025) AS Commission
FROM [Transaction] t
JOIN Employee e ON t.eID = e.eID
GROUP BY t.Amount_Due

Probably just a typo - the JOIN should probably be ON t.eID = e.eId . You need to include both tables (table aliases) involved - you're currently just using the t.eID twice.

Try this:

SELECT 
    t.Amount_Due, SUM(t.Amount_Due*0.025) AS Commission
FROM 
    [Transaction] t
JOIN 
    Employee e ON t.eID = e.eID
GROUP BY 
    t.Amount_Due

As pointed out by @charlieface - it seems very odd that you're summing the Amount_due , and at the same time trying to group by that same column..... my best guess is that you probably want to group by the employee, based on their eID - so use something like this:

SELECT 
    e.eID, e.fName, e.lName, SUM(t.Amount_Due*0.025) AS Commission
FROM 
    [Transaction] t
JOIN 
    Employee e ON t.eID = e.eID
GROUP BY
    e.eID, e.fName, e.lName

for that.

You are grouping by the wrong columns, and you are trying to select Amount_Due and aggregate it at the same time:

SELECT e.fName
    , e.lName,
    , SUM(t.Amount_Due * 0.025) AS Commission
    FROM [Transaction] t
    JOIN Employee e 
      ON t.eID = e.eID
    GROUP BY e.ID, e.fName, e.lName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM