我有这段代码:
SELECT q.HospitalNumber, q.Patient_Name,
q.[Date/Time]) as DATE_OF_GCS, --convert(varchar(5),q.[Date/Time],108) as TIME_OF_GCS,
max(q.[GCS_COUNT]) as BEST_GCS
From
(
Select pat.HospitalNumber, pat.FirstName + ' ' + pat.LastName as Patient_Name, ts.Time as [Date/Time], sum(pt.value) as [GCS_COUNT]
from ParametersText pt INNER JOIN TextSignals ts ON ts.TextID = pt.TextID AND ts.ParameterID = pt.ParameterID
INNER JOIN Patients pat ON pat.patientID = ts.PatientID
WHERE ts.ParameterID = 21654 or ts.ParameterID = 21655 or ts.ParameterID = 21656
GROUP BY pat.HospitalNumber, pat.FirstName, pat.LastName, ts.PatientID, ts.Time
) q
GROUP BY q.HospitalNumber, q.Patient_Name, q.[Date/Time]
--,convert(varchar(5),q.[Date/Time],108)
但我所追求的是每位患者每天最高的Best_GCS,但我也需要时间。 GCS的录制可以在一天中多次进行,并且可以多次进行相同的分数。任何帮助都将非常感激......
非常感谢...
这是在SQL Server(t-sql)
上以下是此查询抛出的数据片段:
patientID Patient_Name DATE_OF_GCS GCS
442 patient name 2014-02-13 16:00:00.000 15
442 patient name 2014-02-13 18:00:00.000 15
442 patient name 2014-02-13 20:00:00.000 15
442 patient name 2014-02-14 00:00:00.000 15
442 patient name 2014-02-14 04:00:00.000 15
442 patient name 2014-02-14 05:00:00.000 15
442 patient name 2014-02-14 06:00:00.000 15
442 patient name 2014-02-14 08:00:00.000 15
442 patient name 2014-02-14 12:00:00.000 15
442 patient name 2014-02-14 16:00:00.000 15
442 patient name 2014-02-14 17:00:00.000 15
442 patient name 2014-02-14 20:00:00.000 15
442 patient name 2014-02-14 23:00:00.000 15
442 patient name 2014-02-15 00:00:00.000 15
442 patient name 2014-02-15 02:00:00.000 15
442 patient name 2014-02-15 05:00:00.000 15
442 patient name 2014-02-15 08:00:00.000 15
442 patient name 2014-02-15 12:00:00.000 15
442 patient name 2014-02-15 15:00:00.000 11
442 patient name 2014-02-15 16:00:00.000 15
442 patient name 2014-02-15 17:00:00.000 15
442 patient name 2014-02-15 18:00:00.000 15
442 patient name 2014-02-15 20:00:00.000 15
442 patient name 2014-02-16 00:00:00.000 15
442 patient name 2014-02-16 02:00:00.000 15
442 patient name 2014-02-16 05:00:00.000 15
442 patient name 2014-02-16 08:00:00.000 15
442 patient name 2014-02-16 20:00:00.000 11
442 patient name 2014-02-16 20:51:00.000 4
442 patient name 2014-02-17 01:00:00.000 15
442 patient name 2014-02-17 02:00:00.000 15
442 patient name 2014-02-17 04:00:00.000 15
442 patient name 2014-02-17 06:00:00.000 15
442 patient name 2014-02-17 08:00:00.000 15
442 patient name 2014-02-17 10:00:00.000 15
442 patient name 2014-02-17 15:00:00.000 15
442 patient name 2014-02-17 18:00:00.000 15
442 patient name 2014-02-17 20:00:00.000 15
442 patient name 2014-02-18 00:00:00.000 15
442 patient name 2014-02-18 04:00:00.000 15
442 patient name 2014-02-18 08:00:00.000 15
442 patient name 2014-02-18 12:00:00.000 15
442 patient name 2014-02-18 14:00:00.000 15
442 patient name 2014-02-18 15:00:00.000 15
442 patient name 2014-02-18 17:00:00.000 15
442 patient name 2014-02-18 19:00:00.000 15
442 patient name 2014-02-18 20:00:00.000 11
442 patient name 2014-02-19 02:00:00.000 15
442 patient name 2014-02-19 06:00:00.000 15
442 patient name 2014-02-19 09:00:00.000 15
471 patient name 2014-02-13 09:00:00.000 7
471 patient name 2014-02-13 11:00:00.000 7
471 patient name 2014-02-13 13:00:00.000 7
471 patient name 2014-02-13 15:00:00.000 8
471 patient name 2014-02-13 17:00:00.000 8
471 patient name 2014-02-13 19:00:00.000 7
471 patient name 2014-02-13 21:00:00.000 5
471 patient name 2014-02-13 22:00:00.000 5
471 patient name 2014-02-14 00:00:00.000 5
471 patient name 2014-02-14 02:00:00.000 5
471 patient name 2014-02-14 04:00:00.000 5
471 patient name 2014-02-14 06:00:00.000 5
471 patient name 2014-02-14 08:00:00.000 9
471 patient name 2014-02-14 10:00:00.000 6
471 patient name 2014-02-14 12:00:00.000 6
471 patient name 2014-02-14 14:00:00.000 8
471 patient name 2014-02-14 16:00:00.000 6
471 patient name 2014-02-14 18:00:00.000 6
471 patient name 2014-02-14 20:00:00.000 5
471 patient name 2014-02-14 22:00:00.000 6
471 patient name 2014-02-15 00:00:00.000 6
471 patient name 2014-02-15 02:00:00.000 6
471 patient name 2014-02-15 04:00:00.000 6
471 patient name 2014-02-15 06:00:00.000 6
471 patient name 2014-02-15 08:00:00.000 8
471 patient name 2014-02-15 09:00:00.000 6
471 patient name 2014-02-15 10:00:00.000 6
471 patient name 2014-02-15 11:00:00.000 3
471 patient name 2014-02-15 13:00:00.000 5
471 patient name 2014-02-15 14:00:00.000 3
471 patient name 2014-02-15 16:00:00.000 3
471 patient name 2014-02-15 18:00:00.000 3
471 patient name 2014-02-15 19:00:00.000 3
471 patient name 2014-02-15 21:00:00.000 3
471 patient name 2014-02-15 22:00:00.000 3
471 patient name 2014-02-16 00:00:00.000 3
471 patient name 2014-02-16 02:00:00.000 3
471 patient name 2014-02-16 02:30:00.000 3
471 patient name 2014-02-16 03:00:00.000 3
471 patient name 2014-02-16 06:00:00.000 3
471 patient name 2014-02-16 08:00:00.000 3
471 patient name 2014-02-16 12:00:00.000 5
471 patient name 2014-02-16 14:00:00.000 3
471 patient name 2014-02-16 18:00:00.000 3
471 patient name 2014-02-16 19:00:00.000 3
471 patient name 2014-02-16 21:00:00.000 3
472 patient name 2014-02-13 08:00:00.000 15
472 patient name 2014-02-13 12:00:00.000 15
472 patient name 2014-02-13 15:00:00.000 15
472 patient name 2014-02-13 19:00:00.000 15
472 patient name 2014-02-13 22:00:00.000 15
472 patient name 2014-02-14 03:00:00.000 15
472 patient name 2014-02-14 08:00:00.000 15
472 patient name 2014-02-14 14:00:00.000 15
472 patient name 2014-02-14 17:00:00.000 15
472 patient name 2014-02-14 19:00:00.000 15
472 patient name 2014-02-14 21:00:00.000 15
472 patient name 2014-02-14 23:00:00.000 15
472 patient name 2014-02-15 01:00:00.000 15
472 patient name 2014-02-15 05:00:00.000 14
472 patient name 2014-02-15 07:00:00.000 15
472 patient name 2014-02-15 08:00:00.000 15
472 patient name 2014-02-15 20:00:00.000 15
472 patient name 2014-02-15 22:00:00.000 15
472 patient name 2014-02-16 00:00:00.000 15
472 patient name 2014-02-16 03:00:00.000 15
472 patient name 2014-02-16 05:00:00.000 15
472 patient name 2014-02-16 07:00:00.000 15
472 patient name 2014-02-16 09:00:00.000 15
472 patient name 2014-02-16 12:00:00.000 15
472 patient name 2014-02-16 15:00:00.000 15
472 patient name 2014-02-16 18:00:00.000 15
472 patient name 2014-02-16 20:00:00.000 15
472 patient name 2014-02-16 22:00:00.000 15
472 patient name 2014-02-17 00:00:00.000 15
472 patient name 2014-02-17 02:00:00.000 15
472 patient name 2014-02-17 04:00:00.000 15
答案 0 :(得分:0)
我明白了。非常感谢..
这是代码。如果有任何可能更好的方式,我很乐意听到它:
With t as
(
SELECT q.HospitalNumber, q.Patient_Name,
q.[Date/Time] as DATE_OF_GCS,
max(q.[GCS_COUNT]) as BEST_GCS
From
(
Select pat.HospitalNumber, pat.FirstName + ' ' + pat.LastName as Patient_Name, ts.Time as [Date/Time], sum(pt.value) as [GCS_COUNT]
from ParametersText pt INNER JOIN TextSignals ts ON ts.TextID = pt.TextID AND ts.ParameterID = pt.ParameterID
INNER JOIN Patients pat ON pat.patientID = ts.PatientID
WHERE ts.ParameterID = 21654 or ts.ParameterID = 21655 or ts.ParameterID = 21656
GROUP BY pat.HospitalNumber, pat.FirstName, pat.LastName, ts.PatientID, ts.Time
) q
GROUP BY q.HospitalNumber, q.Patient_Name, q.[Date/Time]
)
Select hospitalnumber, Date_Of_GCS, Best_GCS
FROM (
select t.hospitalnumber, t.Date_Of_GCS, t.Best_GCS, ROW_NUMBER() over (partition by t.hospitalnumber, cast(t.Date_Of_GCS as date) order by t.Best_GCS Desc ) as GCS
FROM t ) as p
where gcs = 1
Order By hospitalnumber, Date_Of_GCS