Skip to content
Advertisement

My sums are way too huge, is it not possible to do them all at once?

I have a table with colleague info, including their IDs, and a separate table with transaction data (such as revenue).

In the transaction table, there are three fields (code1, code2, code3), they could be blank, or have colleague IDs, etc. I have no control over these tables.

I am trying to accumulate some sums by colleague, so right now I have things like this:

Basically summing up values wherever their codes are found in the table for each code, which I’ve already done using the SUMIFS function on the same data in Excel so I have something to compare to, and normally I could write three separate queries, one for each sum, and then join those in later, but I am trying to do it all in one query.

However the sums that SQL are returning are huge, is there an obvious reason why?

Advertisement

Answer

I doubt you really want a Cartesian product of the transactions. Instead, I think you just want to pick colleagues out of three columns and add up the revenue for which column they are in.

This is easy to do with APPLY and GROUP BY:

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