SQL Server按聚合选择一列,而不是另一列

时间:2017-05-17 14:38:44

标签: sql sql-server database select

对sql不好。如果问题不是100%清楚,请原谅我。这是我的查询

SELECT 
     MAX(PatientId),
  [Date],
  [Time],
  CASE WHEN MAX(CAST(HealthScoreSkipped as INT)) = 1
    THEN '--'
    ELSE MAX(DailyHealthScore)
  END DailyHealthScore,
  ProtocolGroupName,
  MAX(BloodPressure) BloodPressure,
  MAX(SystolicAlert) SystolicAlert,
  MAX(DiastolicAlert) DiastolicAlert,
  MAX(BloodPressureSkipped) BloodPressureSkipped,
  MAX(Pulse) Pulse,
  MAX(PulseAlert) PulseAlert,
  MAX(PulseSkipped) PulseSkipped,
  MAX(BloodSugar) BloodSugar,
  MAX(BloodSugarAlert) BloodSugarAlert,
  MAX(BloodSugarSkipped) BloodSugarSkipped,
  MAX(Steps) Steps,
  MAX(StepsAlert) StepsAlert,
  MAX(StepsSkipped) StepsSkipped,
  MAX(O2) O2,
  MAX(O2Alert) O2Alert,
  MAX(O2Skipped) O2Skipped,
  MAX(Weight) Weight,
  MAX(WeightAlert) WeightAlert,
  @BaselineWeight AS BaselineWeight,
  MAX(WeightSkipped) WeightSkipped,
  MAX(Temperature) Temperature,
  MAX(TemperatureAlert) TemperatureAlert,
  MAX(TemperatureUnit) TemperatureUnit,
  MAX(TemperatureSkipped) TemperatureSkipped,
  MAX(PEF) PEF,
  MAX(PEFAlert) PEFAlert,
  MAX(PEFSkipped) PEFSkipped,
  MAX(FEV1) FEV1,
  MAX(FEV1Alert) FEV1Alert,
  MAX(FEV1Skipped) FEV1Skipped,
  MAX(FEVRatio) FEVRatio,
  MAX(FEVRatioAlert) FEVRatioAlert,
  MAX(FEVRatioSkipped) FEVRatioSkipped,
  @SpiroEnabled SpiroEnabled
   FROM @bioAndScores
    GROUP BY PatientId, Date, Time, ProtocolGroupName

问题在于

MAX(Steps) Steps,
MAX(StepsAlert) StepsAlert

我想选择最大步数,但是该行的stepalert值不是stepAlert的最大值。

4 个答案:

答案 0 :(得分:1)

您可以在select语句中创建子查询,以获取与您的步骤对应的步骤警报。 下面的内容(注意我不确定你为什么要通过patientId进行分组,如果你想要按照患者id分组,那么如果你想要按max(patientId)分组,那么子查询的where子句也应该匹配患者Id

SELECT 
    MAX(bas.PatientId),
    bas.[Date],
    bas.[Time],
    bas.ProtocolGroupName,
    .
    .
    .
    MAX(bas.Steps) Steps,
    --sub query to get the StepsAlert that corresponds to max steps
    (SELECT 
        StepsAlert 
    FROM 
        @bioAndScores subBas 
    WHERE
        --This is the important part of finding the match for Max Steps
        MAX(bas.Steps) = subBas.Steps AND
        --commented out because the MAX(PatientId) was ambiguous
        --bas.PatientId = subBas.PatientId AND
        bas.[Date] = subBas.[Date] AND
        bas.[Time] = subBas.[Time] AND
        bas.ProtocolGroupName = subBas.ProtocolGroupName) as StepsAlert 
FROM
    @bioAndScores as bas
GROUP BY
    --PatientId, 
    bas.Date, 
    bas.Time, 
    bas.ProtocolGroupName

答案 1 :(得分:0)

从StepAlerts中删除MAX()函数,并将StepAlerts添加到GROUP BY子句中。

MAX(Steps) AS Steps,
StepsAlert AS StepsAlert

在你的GROUP BY:

GROUP BY PatientId, Date, Time, ProtocolGroupName, StepAlerts

答案 2 :(得分:0)

只需将StepsAlert列添加到group by子句中,然后移除MAX聚合函数。

GROUP BY PatientId, Date, Time, ProtocolGroupName,StepsAlert 

我建议你通过this来更好地了解群组是如何运作的。

答案 3 :(得分:0)

您可以使用apply()执行此操作,以选择与最早Steps最高StepsAlert对应的值集,如下所示:

select 
    PatientId
  , [Date]
  , [Time]
  , DailyHealthScore    = case 
      when MAX(CAST(HealthScoreSkipped as int))= 1 then '--' 
      else MAX(DailyHealthScore) 
      end 
  , ProtocolGroupName
  , BloodPressure       = MAX(BloodPressure)
  , SystolicAlert       = MAX(SystolicAlert)
  , DiastolicAlert      = MAX(DiastolicAlert)
  , BloodPressureSkipped= MAX(BloodPressureSkipped)
  , Pulse               = MAX(Pulse)
  , PulseAlert          = MAX(PulseAlert)
  , PulseSkipped        = MAX(PulseSkipped)
  , BloodSugar          = MAX(BloodSugar)
  , BloodSugarAlert     = MAX(BloodSugarAlert)
  , BloodSugarSkipped   = MAX(BloodSugarSkipped)
  , Steps               = x.Steps
  , StepsAlert          = x.StepsAlert
  , StepsSkipped        = MAX(StepsSkipped)
  , O2                  = MAX(O2)
  , O2Alert             = MAX(O2Alert)
  , O2Skipped           = MAX(O2Skipped)
  , Weight              = MAX(Weight)
  , WeightAlert         = MAX(WeightAlert)
  , BaselineWeight      = @BaselineWeight
  , WeightSkipped       = MAX(WeightSkipped)
  , Temperature         = MAX(Temperature)
  , TemperatureAlert    = MAX(TemperatureAlert)
  , TemperatureUnit     = MAX(TemperatureUnit)
  , TemperatureSkipped  = MAX(TemperatureSkipped)
  , PEF                 = MAX(PEF)
  , PEFAlert            = MAX(PEFAlert)
  , PEFSkipped          = MAX(PEFSkipped)
  , FEV1                = MAX(FEV1)
  , FEV1Alert           = MAX(FEV1Alert)
  , FEV1Skipped         = MAX(FEV1Skipped)
  , FEVRatio            = MAX(FEVRatio)
  , FEVRatioAlert       = MAX(FEVRatioAlert)
  , FEVRatioSkipped     = MAX(FEVRatioSkipped)
  , SpiroEnabled        = @SpiroEnabled
from @bioAndScores b
  cross apply (
    select top 1
        i.Steps
      , i.StepsAlert
    from @bioAndScores i
    where b.PatientId = i.PatientId
      and b.[Date] = i.[Date]
      and b.[Time] = i.[Time]
      and b.ProtocolGroupName = i.ProtocolGroupName
    order by i.Steps desc, i.StepsAlert asc
      ) x
group by 
    PatientId
  , date
  , time
  , ProtocolGroupName