Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL选择日期范围内的平均分数_Mysql_Sql_Postgresql - Fatal编程技术网

Mysql SQL选择日期范围内的平均分数

Mysql SQL选择日期范围内的平均分数,mysql,sql,postgresql,Mysql,Sql,Postgresql,我有三张桌子: doctors (id, name) -> has_many: patients (id, doctor_id, name) -> has_many: health_conditions (id, patient_id, note, created_at) 每天,每个患者都会得到一个健康状况,并附上从1到10的注释,其中10是一个良好的健康状态,如果可以的话,完全恢复 我想提取的是过去30天内的以下3项统计数据: -有多少病人好转了 -有多少

我有三张桌子:

doctors (id, name) -> has_many:
    patients (id, doctor_id, name) -> has_many:
        health_conditions (id, patient_id, note, created_at)
每天,每个患者都会得到一个健康状况,并附上从1到10的注释,其中10是一个良好的健康状态,如果可以的话,完全恢复

我想提取的是过去30天内的以下3项统计数据: -有多少病人好转了 -有多少病人病情最严重 -有多少患者保持不变

这些统计数据是全局性的,所以我现在不关心每个医生的统计数据,只要查询正确,我就可以提取这些数据

诀窍在于,查询需要提取当前的健康状况注释,并与本月没有今天的过去几天的平均值进行比较,因此需要提取今天的注释以及除此之外的其他几天的平均值

我认为查询不需要定义谁上/下/相同,因为我可以循环并决定这一点。我想今天和本月剩下的时间就足够了

以下是迄今为止obv不起作用的内容,因为由于应用的限制,它只返回一个结果:

SELECT
    p.id,
    p.name,
    hc.latest,
    hcc.average
FROM
    pacients p
INNER JOIN (
        SELECT
            id,
            pacient_id,
            note as LATEST
        FROM
            health_conditions
        GROUP BY pacient_id, id
        ORDER BY created_at DESC
        LIMIT 1
    ) hc ON(hc.pacient_id=p.id)
INNER JOIN (
        SELECT
            id,
            pacient_id,
            avg(note) AS average
        FROM
            health_conditions
        GROUP BY pacient_id, id
    ) hcc ON(hcc.pacient_id=p.id AND hcc.id!=hc.id)
WHERE
    date_part('epoch',date_trunc('day', hcc.created_at))
    BETWEEN
        (date_part('epoch',date_trunc('day', hc.created_at)) - (30 * 86400))
    AND
        date_part('epoch',date_trunc('day', hc.created_at))

查询具有区分最新数据和平均数据所需的所有逻辑,但这种限制会扼杀一切。我需要该限制来提取用于与过去结果进行比较的最新结果。

类似于这样的内容,假设创建时间为日期类型

这是PostgreSQL语法。我不确定MySQL是否以同样的方式支持日期算法

编辑:

这将为您提供每位患者的最新记录,以及过去30天的平均值:

select p.name,
       hc.created_at as last_note_date
       hc.note as current_note,
       t.avg_note
from patients p
   join health_conditions hc 
     on hc.patient_id = p.id
    and hc.created_at = (select max(created_at) 
                         from health_conditions hc2 
                         where hc2.patient_id = hc.patient_id)
   join (
      select patient_id, 
             avg(note) as avg_note
      from health_conditions hc3
      where created_at between current_date - 30 and current_date - 1
      group by patient_id
    ) t on t.patient_id = hc.patient_id

在MySQL中,您可以使用date_subyourdate执行日期算术,间隔20天。对于当前日期,您可以使用当前_日期。问题是获取病情恶化患者的数量,保持不变并有所改进…@eggyal:问题是:我不认为查询需要定义谁上/下/相同,因为我可以循环并决定,就在今天和本月的其他时间就足够了。我猜谢谢你的输入@a_horse_,没有名字和@eggyal。你的问题看起来不错,但我也遇到了一个问题。如果管理员在添加pacient Status之前运行报告,因此今天没有条目,那么他将看不到任何内容,因为您的条件hc.created_at=current_date会对其进行筛选。@GigiContra:查询无法显示,没有什么内容。或者,如果今天没有空值,您希望看到当前便笺的空值吗?我也将尝试这种方法。谢谢@eggyal
select p.name,
       hc.created_at as last_note_date
       hc.note as current_note,
       t.avg_note
from patients p
   join health_conditions hc 
     on hc.patient_id = p.id
    and hc.created_at = (select max(created_at) 
                         from health_conditions hc2 
                         where hc2.patient_id = hc.patient_id)
   join (
      select patient_id, 
             avg(note) as avg_note
      from health_conditions hc3
      where created_at between current_date - 30 and current_date - 1
      group by patient_id
    ) t on t.patient_id = hc.patient_id
SELECT SUM(delta < 0) AS worsened,
       SUM(delta = 0) AS no_change,
       SUM(delta > 0) AS improved
FROM  (
  SELECT   patient_id,
           SUM(IF(DATE(created_at) = CURDATE(),note,NULL))
         - AVG(IF(DATE(created_at) < CURDATE(),note,NULL)) AS delta
  FROM     health_conditions
  WHERE    DATE(created_at) BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
  GROUP BY patient_id
) t