简体   繁体   English

单个表的平均出生日期(Oracle SQL)

[英]Avg date of birth year from single table (oracle sql)

Average date of birth from a single table where the column's date format is '01-JAN-2001'. 单个表格的平均出生日期,该表格的日期格式为“ 01-JAN-2001”。 Looking to return total count of users, and the average DOB YEAR (oracle sql) 寻找返回的用户总数,以及平均DOB年(oracle sql)

mock table: 模拟表:

|User_ID|birth_date|
|123|01-JAN-2001|
|123|01-JAN-2001|
|123|01-JAN-2001|

Assuming your intention is to just average the year, you can parse it to a date, extract the year and average it as a number: 假设您打算仅对年份进行平均,则可以将其解析为一个日期,提取年份并将其平均为一个数字:

SELECT COUNT(*), AVG(EXTRACT(YEAR FROM TO_DATE(birth_date, 'dd-mon-yyyy'))
FROM   users

(Oracle 12c:) Test table and data: (Oracle 12c :)测试表和数据:

create table dt (
  userid_ number generated always as identity ( start with 123 )
, d_ date
);

begin
  for i in 1 .. 15
  loop
    insert into dt ( d_ ) values (
      to_date ( trunc( dbms_random.value( 2446067, 2458100 ) ), 'J' )
    ) ;
   end loop;
end;
/

SQL> select userid_, to_char( d_, 'DD-MON-YYYY' ) dob from dt;

   USERID_ DOB                 
---------- --------------------
       123 19-JUN-2000         
       124 06-OCT-2005         
       125 27-JAN-2012         
       126 09-JUL-2003         
       127 23-JUL-2010         
       128 07-FEB-1992         
       129 20-DEC-2002         
       130 19-MAY-2002         
       131 23-FEB-1990         
       132 26-DEC-1990         
       133 19-JUN-1999         
       134 16-DEC-1994         
       135 13-APR-2017         
       136 31-MAR-2000         
       137 23-MAY-1987

Query and result: 查询和结果:

select 
  count(*) user_count
, trunc( avg( extract( year from d_ ) ) ) avg_dob_year
from dt ;

USER_COUNT AVG_DOB_YEAR
---------- ------------
        15         2000

(Not taking "days" into account, just "years"). (不考虑“天”,仅考虑“年”)。

When using the method suggested by @mathguy 使用@mathguy建议的方法时

"... It can be calculated by subtracting a fixed date, like 2000/01/01, from all dates, taking the average of the differences, and adding it to the fixed date." “ ...可以通过从所有日期中减去固定日期(例如2000/01/01),取差值的平均值,然后将其加到固定日期来计算。”

... this may be a query we could start with (I'm sure it can be refined): ...这可能是我们可以开始的查询(我敢肯定它可以改进):

select
  count(*) usercount
, to_date( '2000/01/01', 'YYYY/MM/DD' ) fixeddate
, avg( differences ) difftofixed
, to_date('2000/01/01', 'YYYY/MM/DD' ) + avg( differences ) fixedplusdiff
, extract( year from ( to_date('2000/01/01', 'YYYY/MM/DD' ) + avg( differences ) ) ) dob_year
from (
  select 
    d_ - to_date( '2000/01/01', 'YYYY/MM/DD' ) differences
  from dt 
);

-- result
USERCOUNT  FIXEDDATE  DIFFTOFIXED  FIXEDPLUSDIFF  DOB_YEAR  
15         01-JAN-00  250.6        07-SEP-00      2000 

I don't know why you're trying to get the average date but I think it would go something like this : 我不知道您为什么要尝试获取平均日期,但我认为这样的事情会发生:

“SELECT COUNT(user_id), AVG(EXTRACT(YEAR FROM TO_DATE(birth_date, 'dd-MM-yyyy'))
FROM users”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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