简体   繁体   中英

Fetching records from one table with different conditions

Hi i am working with postgresql database. I need to calculate the average time between the records what i am fetching from one table.

Schema:

create_table "user"  do |t|
t.text     "status"
t.datetime "time"
t.datetime "updated_at"

end

Example:

  1. select time from users where status = 'now';
  2. select time from users where status = 'after';

on results of above queries i need to perform average time. Probably i need to combine those two queries into one query. I tried with union operator, whether it is right way to use?. So any help is appreciated..

You should consider using SQL SELECT AVG() function, for each group. eg,

select status, avg(time)  from users where status = 'now' or status = 'after' group by status

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