PostgreSQL: calculating multiple averages in one query -


i'd calculate average amount of money spent per day past 7, 30, 90 , 180 days. know how using pl/pgsql, i'd prefer in 1 query if possible. this:

select sum(amount)/days transactions created_at > current_date - ((days || ' day')::interval) , days = any(array[7,30,90,180]); 

error: column "days" not exist

you can use unnest convert array table , use correlated subquery calculate average:

select     days,     (select sum(amount)/days      transactions      created_at > current_date - ((days || ' day')::interval)     ) average unnest(array[7,30,90,180]) t(days) 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -