如何通过最早的日期sql获得平均值?

时间:2015-02-07 18:20:31

标签: sql oracle

这是我的表:

CREATE TABLE Consultation (
idConsultation number,
dateConsultation date NOT NULL,
prixConsultation number(4,2),
pouls number(3),
size number(3),
poids number(3),
pressionSystolique number(3),
pressionDiastolique number(3),
idMedecin number(3),
idPatient number(3),
);

患者可以在此表中出现多次,咨询有约会。

我希望得到结果的平均值,并尊重我们只能采用每位患者咨询的较早日期这一事实。

提前致谢,这就是我的到来,但没有工作......

SELECT AVG(size)
FROM Consultation
WHERE dateConsultation = (SELECT MAX(dateConsultation) FROM Consultation);

3 个答案:

答案 0 :(得分:0)

WITH CTE AS 
(
 SELECT * 
      , ROW_NUMBER() OVER (PARTITION BY idPatient ORDER BY dateConsultation) rn 
 FROM Consultation 
)
SELECT idPatient 
      ,AVG(size) Avg_Size
FROM CTE 
WHERE rn > 1
GROUP BY idPatient;

答案 1 :(得分:0)

您希望首先获得max(dateConsultation)idPatient,然后使用它来过滤avg(size) select。您可以使用子查询和inner join

执行此操作
select avg(c.size) as avgSizeLatestConsultation
from Consultation as c
    inner join (select
                    idPatient,
                    max(dateConsultation) as LatestConsultation
                from Consultation
                group by
                    idPatient) as d
         on d.idPatient = c.idPatient
         and d.LatestConsultation = c.dateConsultation

答案 2 :(得分:0)

鉴于此测试数据:

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(1,  10, TO_DATE('01-FEB-2015', 'DD-MON-YYYY'), 1);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(2,  10, TO_DATE('02-FEB-2015', 'DD-MON-YYYY'), 3);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(3,  11, TO_DATE('03-FEB-2015', 'DD-MON-YYYY'), 1);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(4,  12, TO_DATE('04-FEB-2015', 'DD-MON-YYYY'), 1);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(5,  13, TO_DATE('05-FEB-2015', 'DD-MON-YYYY'), 1);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(6,  13, TO_DATE('06-FEB-2015', 'DD-MON-YYYY'), 2);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(7,  13, TO_DATE('07-FEB-2015', 'DD-MON-YYYY'), 3);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(8,  14, TO_DATE('08-FEB-2015', 'DD-MON-YYYY'), 1);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(9,  15, TO_DATE('09-FEB-2015', 'DD-MON-YYYY'), 1);

INSERT INTO CONSULTATION (IDCONSULTATION, IDPATIENT, DATECONSULTATION, "size")
  VALUES(10, 15, TO_DATE('10-FEB-2015', 'DD-MON-YYYY'), 12);

以下查询生成正确的结果(AVG_SIZE = 1):

SELECT AVG("size") AS AVG_SIZE
  FROM CONSULTATION
  WHERE IDCONSULTATION IN (SELECT IDCONSULTATION
                             FROM CONSULTATION
                             WHERE (IDPATIENT, DATECONSULTATION) IN
                               (SELECT IDPATIENT, MIN(DATECONSULTATION) AS DATECONSULTATION
                                  FROM CONSULTATION
                                  GROUP BY IDPATIENT));

SQLFiddle here

分享并享受。