简体   繁体   中英

postgresql union

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      |
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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM