MYSQL中遇到的问题以及解决方法排序后筛选+横转纵

Posted LuckyKun的羊肉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL中遇到的问题以及解决方法排序后筛选+横转纵相关的知识,希望对你有一定的参考价值。

采集方式有KRK和YD

将YD的数据和KRK的放在 一列,但是可能YD有两条,那么如何选择YD最新的一条和KRK结合起来呢?先筛选后横转纵

-- KRK 和YD组合到一起
select PatientID ,  Age, Sex, Height, Weight,OperatorID,BMI,
max(case when KRKPPGFilePath is not null then KRKPPGFilePath else ‘‘ end) KRKPPGFilePath , 
max(case when KRKPPGFilePath is not null then SBP else ‘‘ end) KRK_SBP,
max(case when KRKPPGFilePath is not null then DBP else ‘‘ end) KRK_DBP,
max(case when PPGFilePath is not null then PPGFilePath else ‘‘ end) PPGFilePath1 , 
max(case when PPGFilePath is not null then SBP else ‘‘ end) YD_SBP,
max(case when PPGFilePath is not null then DBP else ‘‘ end) YD_DBP,
max(case when PPGFilePath is not null then Ecgdisease else ‘‘ end) Ecgdisease,
max(case when PPGFilePath is not null then EcgHRV else ‘‘ end) EcgHRV
from (
select     p.PatientID ,p.PPGFilePath, p.KRKPPGFilePath, p.SBP, p.DBP, pa.Age, pa.Sex, pa.Height, pa.Weight,pa.BMI,p.OperatorID,p.Ecgdisease,p.EcgHRV
FROM
    ppg_data AS p
    LEFT JOIN 
    (select *,pa.Weight/power(pa.Height,2)*10000 as BMI from patient AS pa)pa ON p.PatientID=pa.PatientID 
    LEFT JOIN operator AS o ON o.OperatorID = p.OperatorID
    where   p.PatientID not  IN (SELECT PatientID from patient_fortest)
    and p.PatientID in (
    select A.PatientID from (    select * from (select * from
     (SELECT
    p.DataID,p.Quality,p.PatientID , p.PPGFilePath, p.SBP, p.DBP, pa.Age, pa.Sex, pa.Height, pa.Weight,round( pa.BMI,2)BMI,p.ECGFilePath, p.OperatorID
FROM
    ppg_data AS p
    LEFT JOIN 
    (select *,pa.Weight/power(pa.Height,2)*10000 as BMI from patient AS pa)pa ON p.PatientID=pa.PatientID
    LEFT JOIN operator AS o ON o.OperatorID = p.OperatorID
WHERE
    p.TestEndTime >= 2019-12-17 00:00:01 
    AND p.TestEndTime < 2020-07-08 00:00:01 
    AND p.State = 1 
    AND p.Quality=1
    AND pa.Age>=16
    AND pa.Age<=120
    AND pa.Weight>=30
    AND pa.Weight<=160
    AND pa.Height>=130
    AND pa.Height<=220
    AND p.SBP>p.DBP
    AND BMI>=15
    AND BMI<=50
    AND pa.RecordIsDeleted = 0
    AND o.isCensus = 1
    AND p.KRKPPGFilePath IS NOT NULL 
    AND p.SBP IS NOT NULL
ORDER BY p.PatientID)as p order by p.Quality desc,DataID desc
limit 30000)p
group by p.PatientID )A
    join 
    (select * from (select * from
     (SELECT
    p.DataID,p.Quality,p.PatientID , p.PPGFilePath, p.SBP, p.DBP, pa.Age, pa.Sex, pa.Height, pa.Weight,round( pa.BMI,2)BMI,p.ECGFilePath, p.OperatorID
FROM
    ppg_data AS p
    LEFT JOIN 
    (select *,pa.Weight/power(pa.Height,2)*10000 as BMI from patient AS pa)pa ON p.PatientID=pa.PatientID
    LEFT JOIN operator AS o ON o.OperatorID = p.OperatorID
WHERE
    p.TestEndTime >= 2019-12-17 00:00:01 
    AND p.TestEndTime < 2020-07-08 00:00:01 
    AND p.State = 1 
    AND p.Quality=1
    AND pa.Age>=16
    AND pa.Age<=120
    AND pa.Weight>=30
    AND pa.Weight<=160
    AND pa.Height>=130
    AND pa.Height<=220
    AND p.SBP>p.DBP
    AND BMI>=15
    AND BMI<=50
    AND pa.RecordIsDeleted = 0
    AND o.isCensus = 1
    AND p.PPGFilePath IS NOT NULL 
    AND p.SBP IS NOT NULL
ORDER BY p.PatientID)as p order by p.Quality desc,DataID desc
limit 30000)p
group by p.PatientID  )B
    on A.PatientID=B.PatientID )
    and p.TestEndTime >= 2019-12-17 00:00:01 
    AND p.TestEndTime < 2020-07-08 00:00:01 
    AND p.State = 1 
  and p.Quality=1
    AND pa.Age>=16
    AND pa.Age<=120
    AND pa.Weight>=30
    AND pa.Weight<=160
    AND pa.Height>=130
    AND pa.Height<=220
    AND p.SBP>p.DBP
    AND BMI>=15
    AND BMI<=50
    AND pa.RecordIsDeleted = 0
    AND o.isCensus = 1
    AND p.SBP IS NOT NULL
ORDER BY p.PatientID)A
group by  PatientID ,  Age, Sex, Height, Weight,BMI

 

以上是关于MYSQL中遇到的问题以及解决方法排序后筛选+横转纵的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL中遇到的问题以及解决方法排序后筛选

[MYSQL]groupBy筛选优先排序问题

mysql -3练习(分组查询后再次筛选,顺逆序排序)

mysql进阶5:分组查询

MYSQL中遇到的问题以及解决方法分组统计数据

MySQL中遇到的问题以及解决方法出现负值