har en query: <code>query hjälp
<code>
SELECT dbo.Pejl.PejlID AS ID, CustomerName AS Customer, MeasurementObjectName AS 'Measure
Object', ProductName AS Product, MeasurementObjectTypeName AS 'Object Type', MeasurementLogValue AS 'Measured Value', MeasurementLogTimeStamp AS Time, MeasurementLogTemperature AS Temperature
FROM dbo.Pejl
LEFT JOIN dbo.Customer ON dbo.Pejl.CustomerID = dbo.Customer.CustomerID
LEFT JOIN dbo.MeasurementObject ON dbo.Pejl.PejlID = dbo.MeasurementObject.PejlID
LEFT JOIN dbo.Product ON dbo.MeasurementObject.ProductID = dbo.Product.ProductID
LEFT JOIN dbo.MeasurementObjectType ON dbo.MeasurementObject.MeasurementObjectTypeID = dbo.MeasurementObjectType.MeasurementObjectTypeID
LEFT JOIN dbo.MeasurementLog ON dbo.MeasurementObject.MeasurementObjectID =
dbo.MeasurementLog.MeasurementObjectID
</code>
Nu skulle jag vilja att man bara får upp ett värde per ID som är det senaste(MeasurementLogTimeStamp)
Hu rlösa det på bäst sätt?
.pSv: query hjälp
SELECT pe.PejlID AS ID
, c.CustomerName AS Customer
, mo.MeasurementObjectName AS 'Measure Object'
, p.ProductName AS Product
, mot.MeasurementObjectTypeName AS 'Object Type'
, ml.MeasurementLogValue AS 'Measured Value'
, ml.MeasurementLogTimeStamp AS Time
, ml.MeasurementLogTemperature AS Temperature
FROM dbo.Pejl pe
LEFT JOIN dbo.Customer c ON pe.CustomerID = c.CustomerID
LEFT JOIN dbo.MeasurementObject mo ON pe.PejlID = mo.PejlID
LEFT JOIN dbo.Product pr ON mo.ProductID = pr.ProductID
LEFT JOIN dbo.MeasurementObjectType mot ON mo.MeasurementObjectTypeID = mo.MeasurementObjectTypeID
LEFT JOIN (
SELECT MeasureMentObjectId, MAX(MeasurementLogTimeStamp) AS MeasurementLogTimeStamp
FROM dbo.MeasurementLog
GROUP BY MeasureMentObjectId
) mlMax ON mo.MeasurementObjectID = mlMax.MeasurementObjectID
LEFT JOIN dbo.MeasurementLog ml
ON mo.MeasurementObjectID = ml.MeasurementObjectID
AND mlMax.MeasurementLogTimeStamp = ml.MeasurementLogTimeStamp
</code>
Skrivet ur huvudet, så det kan finnas fel.