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
Post a Comment