簡體   English   中英

最大值(值)的 Select 列

[英]Select column of the Max(value)

我有這些桌子

記錄:

+----+-------------+--------------+-------------------------+---------+
| Id | IdHealtcare | ValueMonitor |       DateMonitor       | Checked |
+----+-------------+--------------+-------------------------+---------+
|  1 |           1 |           80 | 2020-05-14 19:40:11.873 | NULL    |
|  2 |           2 |           66 | 2020-05-14 20:56:33.360 | NULL    |
|  3 |           1 |          100 | 2020-05-14 20:32:24.397 | NULL    |
|  4 |           3 |           87 | 2020-05-14 20:57:12.390 | NULL    |
+----+-------------+--------------+-------------------------+---------+

衛生保健:

+----+-----------+----------+--------+------------+
| Id | IdPatient | IdDoctor | IdType | RangeValue |
+----+-----------+----------+--------+------------+
|  1 |         1 |        1 |      1 | 0-180      |
|  2 |         1 |        1 |      2 | 50-200     |
|  3 |         2 |        2 |      1 | 90-180     |
+----+-----------+----------+--------+------------+

我想為每種患者的醫療保健類型 select 提供患者的 ID 和最近的日期值。

我已經嘗試過這些查詢:

select p.CodiceFiscale, p.Surname, p.Name, t.Name, Max(r.DateMonitor), 
   r.ValueMonitor
from dbo.Patients as p
inner join dbo.HealtcareMonitoring as m on m.IdPatient = p.Id 
inner join dbo.Records as r on r.IdHealtcare = m.Id 
inner join dbo.MonitoringTypes as t on t.Id = m.IdType
where m.IdDoctor = 1
group by t.Name, p.CodiceFiscale, p.Surname, p.Name
SELECT m.IdPatient, m.IdType, MAX(r.DateMonitor), r.ValueMonitor AS LastDate
FROM  dbo.Records as r
inner join dbo.HealtcareMonitoring as m on m.Id = r.IdHealtcare  
where m.IdDoctor = 1
GROUP BY m.IdPatient, m.IdType

但我不能 select 該值,因為它不包含在聚合 function 或“分組依據”子句中。

我該如何解決? 有沒有更好的方法來解決這個問題?

你為什么不在group by中添加r.ValueMonitor 它應該修復該錯誤

SELECT m.IdPatient, m.IdType, MAX(r.DateMonitor), r.ValueMonitor AS LastDate
    FROM  dbo.Records as r inner join dbo.HealtcareMonitoring as m
    on m.Id = r.IdHealtcare  
    where m.IdDoctor = 1
    GROUP BY m.IdPatient, m.IdType, r.ValueMonitor 

一種解決方案可以按非聚合列分組

select  p.CodiceFiscale, p.Surname, p.Name, t.Name, Max(r.DateMonitor), r.ValueMonitor from dbo.Patients as p
    inner join dbo.HealtcareMonitoring as m
    on m.IdPatient = p.Id 
    inner join dbo.Records as r
    on r.IdHealtcare = m.Id 
    inner join dbo.MonitoringTypes as t
    on t.Id = m.IdType
    where m.IdDoctor = 1 group by t.Name, p.CodiceFiscale, p.Surname, p.Name
group by p.CodiceFiscale, p.Surname, p.Name, t.Name, r.ValueMonitor

另一種解決方案可能是使用子查詢,但性能更差

select  p.CodiceFiscale, p.Surname, p.Name, t.Name, 
(select MAX(DateMonitor) from dbo.Records where IdHealtcare = m.Id)
, r.ValueMonitor from dbo.Patients as p
    inner join dbo.HealtcareMonitoring as m
    on m.IdPatient = p.Id 
    inner join dbo.MonitoringTypes as t
    on t.Id = m.IdType
    where m.IdDoctor = 1 group by t.Name, p.CodiceFiscale, p.Surname, p.Name

更多關於加入與子查詢的性能在這里SQL 加入與 SQL 子查詢(性能)?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM