views:

36

answers:

2

I have this data in postgresql

  id      | rate      | hrv | activity | orientation |       timestamp        | user_id | status 
----------+-----------+-----+----------+-------------+------------------------+---------+--------------
 66764728 |        72 |   1 |        0 |          90 | 2010-06-10 18:54:54+00 |     397 | t
 66764729 |        72 |   1 |        0 |          90 | 2010-06-10 18:55:09+00 |     397 | t
 66764730 |        76 |   1 |        0 |          90 | 2010-06-10 18:55:23+00 |     397 | t
 66766058 |        68 |   1 |        0 |          90 | 2010-06-10 18:55:38+00 |     397 | t
 66766059 |        72 |   1 |        0 |          90 | 2010-06-10 18:55:53+00 |     397 | t
 66766063 |        80 |   1 |        0 |           0 | 2010-06-10 18:56:51+00 |     397 | t
 66766085 |       100 |   1 |        0 |           0 | 2010-06-10 18:57:06+00 |     397 | t
 66766091 |        -1 |  -1 |     1558 |          90 | 2010-06-10 18:58:34+00 |     397 | t
 66766118 |        -1 |  -1 |     2055 |           0 | 2010-06-10 18:58:49+00 |     397 | t
 66766119 |        -1 |  -1 |     2869 |          90 | 2010-06-10 18:59:03+00 |     397 | t
 66766121 |        -1 |  -1 |     3187 |          90 | 2010-06-10 18:59:18+00 |     397 | t
 66766120 |        -1 |  -1 |     3302 |           0 | 2010-06-10 18:59:33+00 |     397 | t
 66766122 |        -1 |  -1 |     2222 |           0 | 2010-06-10 18:59:47+00 |     397 | t
 66766133 |        60 |   7 |        0 |           0 | 2010-06-10 19:00:16+00 |     397 | t
 66766134 |        64 |   1 |        0 |           0 | 2010-06-10 19:00:31+00 |     397 | t
 66766135 |        72 |   1 |        0 |           0 | 2010-06-10 19:00:46+00 |     397 | t
 66766137 |        72 |   0 |        0 |           0 | 2010-06-10 19:01:15+00 |     397 | t
 66766155 |       132 |   1 |        0 |           0 | 2010-06-10 19:01:59+00 |     397 | t
 66766159 |        -1 |  -1 |     1858 |          90 | 2010-06-10 19:02:58+00 |     397 | t

How do I get?

( ROUND(AVG(rate),1) AS avg_rate, hrv WHERE rate <> -1 ) UNION ( ROUND(AVG(activity),1) AS avg_activity, hrv WHERE activity <> -1 )

into result of single row? Please note 'hrv' is common column AVG(hrv) in final result.

avg_rate   |  avg_activity | AVG(hrv) |
83.1       |  71.2         |   0      |
A: 
SELECT a.avg_rate
     , b.avg_activty
  FROM
(SELECT ROUND(AVG(rate),1) AS avg_rate
 WHERE rate <> -1) a,
(SELECT ROUND(AVG(activity),1) AS avg_activity
 WHERE activity <> -1) b;
dcp
you missed the WHERE condition. It is different for each query.AVG(rate) has one set of rows,AVG(activity) has otherThey are not the same set. I would have very well done a straight query otherwise.
ramonrails
@ramonrails - Sorry for that, check my edited answer.
dcp
Besides that, I want these columns to appear in one row but as 2 columns.
ramonrails
@Ramonrails - I believe what I have should do it (not tested, so there may be some syntax error, but hopefully not :)).
dcp
that worked for me. My SQL is larger though. This was a very simplified version I asked. :) Thanks.
ramonrails
Now comes the interesting part, check updated question
ramonrails
This is called an inline view for future reference.
StarShip3000
A: 

If I understand what you want correctly, this should do it:

select round(avg(case
                     when rate <> -1 then rate
                     else null
                 end), 1) as avg_rate
     , round(avg(case
                     when activity <> -1 then activity
                     else null
                 end), 1) as avg_activity
     , round(avg(hrv)) as avg_hrv
  from my_table
Rudd Zwolinski
how do I get avg(rate), avg(activity), avg(hrv) where all three result sets are different from which they are deriving averages?
ramonrails
The `case` statements allow you to take averages over different parts, by counting the rows you don't want to take into account as null, which doesn't factor into the average. Here, avg_rate is the average value over all rows in the table where the rate isn't -1, for example.
Rudd Zwolinski
Yes, I know the CASE statement. I was asking something different though. Never mind, I got it figured out now. Thanks Rudd, for chiming in :)
ramonrails
Oh, sure. Sorry I couldn't be of more help!
Rudd Zwolinski