sql - Operations on selected items -


i want have query this:

select    sum(data_parts.size) size_sum,   (size_sum/2.) half_of_size_sum    data_parts   data_parts.some_id='1'; 

of course won't work, because there's no column named size_sum, question is:

is there way use size_sum parameter in next select item?

other using subquery containing current query (see davide's answer), don't think there way that.

but do:

select    sum(data_parts.size) size_sum,   (sum(data_parts.size)/2.) half_of_size_sum    data_parts   data_parts.id='1'; 

postgres smart enough sum once. if query expanded more calculations being done on size_sum recommend subquery approach. not because works better, because easier read. if current calculation need, don't bother subquery. less easy read.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -