Skip to content
Advertisement

Use column references in Oracle SQL select query to avoid re-calculation

I want to create a summarised database backup report, which includes data from multiple database tables which has no relation to each other. For example, I want to include the name of the database from v$database view:

And the size of the database:

And consider there is no common column between those two views.

Beside that I want to include the database size in Bytes, KBs, MBs ,……, and so on.

I am using the following query:

In this way I am avoiding the join condition between v$database and v$datafile.

This is just an example, however, in my project I have more than 10 tables. Most of them without a common column.

The question is while converting bytes into kbs, mbs, and so on, I want to use the column references such as:

instead of again and again calculations. Is there any way to use the column references such as mentioned above in the example instead of re-calculation?

Advertisement

Answer

Use a subquery to calculate the results of the sum once in the from clause and then use that:

You can also do this use lateral joins, but it is definitely overkill for this particular example:

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