-1

我有表 tbMeasurement 和 tbPatientMeasurement 。

tb测量

MeasurementIDP
MeasurementName

tb患者测量

PatientMeasurementIDP
MeasurementIDF
MeasurementValue
Taken (Datetime)

执行以下查询时:

SELECT DISTINCT dbo.tbMeasurement.MeasurementName
      , dbo.tbPatientMeasurement.MeasurementValue
      , dbo.tbPatientMeasurement.Taken
  FROM dbo.tbMeasurement
 INNER JOIN dbo.tbPatientMeasurement
   ON  dbo.tbMeasurement.MeasurementIDP = dbo.tbPatientMeasurement.MeasurementIDF

这将返回 MeasurementName 之一的双重条目。我也想要MeasurementName,MeasurementValue by max Taken(datetime).

4

2 回答 2

5

试试这个——

SELECT DISTINCT 
      m.MeasurementName
    , p2.MeasurementValue
    , p2.Taken
FROM dbo.tbMeasurement m
JOIN (
    SELECT 
          p.MeasurementValue
        , Taken = MAX(p.Taken) 
    FROM dbo.tbPatientMeasurement p 
    GROUP BY m.MeasurementName, p.MeasurementValue
) p2 ON m.MeasurementIDP = p2.MeasurementIDF
于 2013-05-28T07:10:16.850 回答
3
SELECT  
      m.MeasurementName
    , p.MeasurementValue
    , a.Taken 
FROM dbo.tbMeasurement m
INNER JOIN dbo.tbPatientMeasurement p ON m.MeasurementIDP = p.MeasurementIDF
INNER JOIN
(
select MeasurementIDF,MAX(Taken) as taken 
from tbPatientMeasurement
group by MeasurementIDF
) a on a.MeasurementIDF=p.MeasurementIDF and a.taken=p.Taken
于 2013-05-28T10:31:33.100 回答