Skip to content
Advertisement

SUM of TotalSellPrice, Grouped by JobID? – Beginner SQL Question

I’m looking to write a query that gives the YTD sales for each job within our system. We use Bistrack. Here is what I have:

This displays a result where each job reference is shown multiple times for each order under that job. I’d like to combine the orders into a SUM for each job. So, for example, I want the blue column to be a single number that is just 3362.68 + 101.03 + 1642.86 + 1298.75.

Example

My idea would be to do SUM(OH.TotalSellPrice – OH.NonSalesAmount) AS “TotalSellPrice”, but that doesn’t work, seemingly because my other columns are not in the function. I think this might be something simple to fix but I just started in SQL. I have gathered that partitioning could be used from browsing this site, but I have no idea how to apply it to my current query. Any help appreciated, thanks!

Advertisement

Answer

From the error message you noted in the comments above, I’m going to assume you’re using SQL Server as a database appliance.

In SQL Server, and most other databases, when you use SUM or any other aggregate functions you are required to add a GROUP BY clause that includes all non-aggregated columns from the SELECT clause of the query.

Plus, have a look at these, and then make informed decisions.

Bad habits : Putting NOLOCK everywhere

Is NOLOCK Ever The Right Choice?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement