Question

I have a Salesman table and a Sales table, I need to get a count of Salesman whose revenue was $1,000,000 based on previous quarter.

The problem I am having is this:

I can do a select of the Sales Table which gets every salesman, then an inner select statement where I take each salesman and find all of his sales. I need to see if all of his sales >= $1,000,000 and I don't know how/if I can do arithmetic inside the select statements to sum of the sales and see if they are >= $1m

Here is my code:

Select count(SalesID) from Salesman SM where SM.SalesID in
(
     Select cost from Sales where salesDate >= beginQtr AND salesDate <= endQtr
     //some code to add them all up and if >= $1m, count that Salesman
); 
Was it helpful?

Solution

There is a function sum(some_column) in sql.
Try something like this:

sum(select cost from sales where salesman_ID = @id) >= 1000000

OTHER TIPS

I think you can try SUM() function

SELECT SUM(column_name) FROM table_name;

this can get you the total sum of a numeric column.

I think this will help

Put the logic to determine if the sum of sales is greater than a million in a HAVING clause.

Something like:

Select SalesID from Sales where salesDate >= beginQtr AND salesDate <= endQtr
GROUP BY salesID
HAVING sum(cost) >= $1m
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top