D
D
des1roer2015-05-14 10:50:08
PostgreSQL
des1roer, 2015-05-14 10:50:08

Postgres average of two columns?

I have a table to store values. that is, one analysis corresponds to several elements with values

SELECT 
  elem_id,
  analiz_id,
  value
FROM 
  vgok_site.a_analiz_data 
  where analiz_id = 26;

7x3cMqx.png
how do i get the average weight of (elem_id(6).value)/(elem_id(5).value) ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pesenka1, 2015-05-14
@des1roer

if I understand you correctly

select cast(x.number as real) / cast(y.number as real)
from
(
  SELECT value as number from a_analiz_data where id = 6
) x
join 
(
  SELECT value as number from a_analiz_data where id = 5
) y on 1=1

A
Alexander, 2015-05-14
@disc

You can add them to a query condition like "WHERE analiz_id = 26 AND elem_id IN (5,6)" and get the value via AVG()

SELECT 
  AVG(value)  
FROM 
  vgok_site.a_analiz_data 
  where analiz_id = 26 AND elem_id IN (5,6) ;

R
RodinUA, 2015-05-18
@RodinUA

Postgres has wonderful lag/lead window functions

SELECT 
  elem_id,
  analiz_id,
  value as this_row_value,
  lag(value,1) OVER () as previous_row_value,
  value/lag(value,1) OVER ()
FROM 
   vgok_site.a_analiz_data 
WHERE
   analiz_id = 26
ORDER BY 
   elem_id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question