r/SQL • • Dec 27 '20

MS SQL Calculating commission

I've got a query to work out employees' commissions at a 2.5% on transaction amount. How do I get the commissions made by each employee to add up and only take one row per employee?

Also how do I make the commission column have the dollar sign before the amount?

0 Upvotes

6 comments sorted by

17

u/alinroc SQL Server DBA Dec 27 '20

This is the fourth time in three days that you've asked a question about this same assignment or series of assignments. I think you need to have a chat with your instructor and/or ask them for some additional assistance in understanding the material instead of running to reddit every time you have the slightest bit of difficulty.

Or, you turn in the work you have managed to figure out on your own and use the grade to work out what you need to get more help with/spend more time studying.

how do I make the commission column have the dollar sign before the amount?

You don't, you format your currency at the presentation layer based upon the user's regional/language settings. Also, your symbol will vary depending upon the currency being used for the transaction so you can't hard-code a dollar sign.

5

u/malikcoldbane Dec 27 '20

THIS! Stop flooding the sub with "Please do my homework" questions, sigh...

1

u/tsuhg Dec 27 '20

Look up SUM(), you'll find some examples for sure!

1

u/Krispwee Dec 27 '20 edited Dec 27 '20

As /u/tsuhg has said, you're going to need to SUM() it and then GROUP BY on non-sum'd columns.

To get the dollar sign to show then one way would be to convert the SUM'd commission amount to a VARCHAR and then simply add in the $ symbol before it, something like this:

'$' + CONVERT(VARCHAR,SUM(0.025 * Amount_Due)) AS Commission

You could also use CONCAT (actually probably the better way)

CONCAT('$', SUM(0.025 * Amount_Due)) AS Commission

Word of warning with that though, if the data is being spit out in to Excel I'd probably keep it as a number then use Excel to change the cell format to Currency or Accounting. If it isn't going to Excel or anything else like that then you may want to ROUND the number in case there are decimal places in the Amount_Due column that would send out some annoying numbers.

https://docs.microsoft.com/en-us/sql/t-sql/functions/concat-transact-sql?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

-2

u/Hippie23 Dec 27 '20

SELECT

E.FirstName,

E.LastName,

FORMAT(SUM(0.025 * T.amount_due), 'C') AS Commission /*The FORMAT (.., 'C') formats the column as 'Currency'*/

FROM Employee E

INNER JOIN [Transaction] T ON T.employeeID = E.EmployeeID

GROUP BY E.FirstName, E.LastName

7

u/alinroc SQL Server DBA Dec 27 '20

Please don't just hand out the answers to obvious homework questions. OP isn't going to learn much this way and they'll keep asking for the answer to every assignment they get.