I'm trying to execute the following SQL:
SELECT * FROM Table2
INNER JOIN
(SELECT * FROM Table1
INNER JOIN
(SELECT MAX(DateTime) AS LastMeasurement, MeasurementId as LastMeasurementId
FROM Table1 GROUP BY MeasurementId) as lastMeasurement
ON (Table1.MeasurementId = lastMeasurement.LastMeasurementId)
AND (Table1.DateTime = lastMeasurement.LastMeasurement)) as hLastMeasurement
ON Table2.Id = hLastMeasurement.Id
ORDER BY Table2.Id ASC
This works just fine, but because I want to get the result in XML format and had to cast the geography column, I've changed the first line to:
SELECT Name, Description, CAST(Location as nvarchar(MAX)) FROM Table2
and added this line to the end:
FOR XML RAW ('Object'), ROOT ('Objects'), ELEMENTS
Now I'm getting the XML, but with data only from Table2.
Any advice will be welcome.