Your Location is: Home > Sql

How do I have the last row displaying an aggregate function in Oracle SQL

From: Romania View: 1497 Henrich 


Is it possible to display an aggregate function like avg on the last row as shown in the picture?

I also want the last row to be "------" if the row is empty. Note that the datatype of values in column A is numeric. (I am sorry if I made things wrong. I am new to StackOverflow)

[Here is how it should look like]

Best answer

I think union all is the simplest method, but I would recommend just using NULL values:

select a, b, c
from t
union all
select '---', 'Average', avg(c)
from t;

SQL does not guarantee the ordering of the rows, but in practice, the avg() is usually last.