1
다른 테이블에 저장된 ids (정수)를 가진 일부 사람의 평균 연령 (년)을 계산하는 plpgsql 함수를 작성하려고합니다.SQL [postgresql]에서 연도의 평균 연령을 계산하는 함수
코드는 다음과 같습니다
물론begin
DROP TABLE if EXISTS dates;
DROP TABLE if EXISTS tmp;
DROP TABLE if EXISTS ages;
CREATE TABLE ages (age integer);
--(...) In these lines, I create and fill the table tmp. I did not include this code
--since it's not very much related to my problem. Nevertheless, this table has only
--one integer column
CREATE TABLE dates AS (SELECT "dateofbirth" from person where "idPerson" in (select "bookedforpersonID" from personsofthistype));
UPDATE ages SET (age) = ((SELECT extract (year from age(dateofbirth)) from dates));
return (select avg(age) from age);
end;
, dateofbirth
유형 date
이다. 내 문제는 만든 테이블 나이 아무것도 포함되어 있지 않으며 따라서 올바른 결과 (해당 열의 평균 수명) 반환 할 수 없다는 것입니다. 함수의 반환 유형은 integer
이며 pgadmin에서는 "반환 값 세트"가 선택되지 않습니다.
PostgreSQL 9.3.4, pgAdmin III 버전 1.18.1을 사용하고 있습니다.
감사합니다.
이것은 끔찍하게 복잡합니다. 생년월일이있는 사람들의 테이블을 갖고 있으며 단순히 평균 연령을 원하십니까? –
예, 맞습니다. – mgus