0

I have a table like below.

|    FILE   | CATEGORY1 | CATEGORY2 | CATEGORY3 |
| File1.txt |    3      |     2     |     6     |
| File2.txt |    4      |     7     |     3     |
| File2.txt |    3      |     1     |     1     |

Now, Is it possible to add a new row as part of select query which will provide the below row added to the resultset.

| Total     |    10     |     10    |     10    |

Expected final output:

|    FILE   | CATEGORY1 | CATEGORY2 | CATEGORY3 |
| File1.txt |    3      |     2     |     6     |
| File2.txt |    4      |     7     |     3     |
| File2.txt |    3      |     1     |     1     |
| Total     |    10     |     10    |     10    |

Any help on achieving the above results is highly appreciated.

Thank you.

1 Answer 1

3

You can use this:

SELECT FILE, CATEGORY1, CATEGORY2, CATEGORY3 From TestTable
union all
SELECT 'Total', Sum([CATEGORY1]), Sum([CATEGORY2]), Sum([CATEGORY3]) From TestTable

If values in your column allow nulls, then can use function like nvl.

You can also try rollup also:

SELECT File, Sum([CATEGORY1]), Sum([CATEGORY2]), Sum([CATEGORY3]) From
TestTable
group by rollup (File);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. I have another query similar to the above one. stackoverflow.com/questions/54644206/… Would be helpful if you can consider checking that as well. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.