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.
Obviously , if you want to display data from other tables then you have to select these columns like this way. IO normally use MYSQL, so this is the way I normally use.But if you are using oracle or other database, then you have to select proper alias names to display custom columns that you wish.see basic example for join using sql server here