Skip to content
Advertisement

How to add multiple columns in current select statement as a new field?

I have the following query:

with this output:

I need to add a balance field that simply adds all of these fields. This is just a sample of a rather long query though. I’m trying to avoid adding each lengthy statement into a (col1 + col2) format, This would seem to slow down a query and this will be querying a production environment soon. Is this my only option? thanks

Advertisement

Answer

The simplest option is a subquery:

As the question and query stand, there is not way you can avoid enumerating the columns you want to sum().

It might be possible, however, to build an additional sum() expression that brings together with or all individual conditions in the existing sum()s. This would look like:

We could work and try to shorten this by factorizing some of the conditions. Depending on your actual data, neater options may be available to shorten the conditions.

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