Skip to content
Advertisement

SQL – Aggregate Functions and Group By

MyView:

Example data within MyView:

I want to SUM the cost for each JobId, but also return the ClientName, ClientAddress, etc.. This view is large, and having to group the ClientName, Client Address is taking a long time to run.

What is the best way to handle this?

The query I currently have is:

Results I’m after:

Advertisement

Answer

If ClientName and ClientAddress are always the same for every JobID, then this (though kludgy) will work:

Under that assumption, all you really want to group by is JobID, as everything else is either invariant or an aggregation target.

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