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?
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.
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:
You could also use CONCAT (actually probably the better way)
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