Skip to content
Advertisement

SQL Query to sum multiple count union queries

I have a statement with following structure, and I’m trying to sum all the count results.

I have tried using the SUM function outside of the nested count queries combined using the same variable declaration for each of the union, but error says that I must give them separate aliases. However, if I do that, won’t I need to refer to these separately within the SUM function?

My SQL Code returning 2 rows:

OUTPUT

Now when I try to SUM the answers I run into trouble:

The error is around using the variable ‘rowtotal’ twice. Surely there’s an easier way to this?

I simply want a sum of both values as:

Advertisement

Answer

You don’t need window functions for that

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