I have a SQL Server database, which logs weather device sensor data.
The table looks like this:
ID instrument sensor ID 1 1 1 42 2 1 1 3 3 1 2 30 4 2 2 5 5 1 2 3 4 26 7 3 1 23 8 3 2 1
In return, the following query should be returned:
ID deviceind sensor ID 2 1 1 3 3 1 2 30 4 2 2 5 5 2 1 1 7 3 1 23 8 3 2 1
Sensors must be unique for each device. That is, in columns, the value must be the device ID and the sensor ID (line-wise).
If I am not clear enough then I apologize.
If you do not want to specify the value
amount as the desired code , You want to take the "arbitrarily" row of each "DeviceId + SensorId" - Group:
with as the CTE (SELECT Id, DeviceId, SensorId, Value, RN = ROW_NUMBER (with the participation by DeviceId, the sensor ID by the ID DESC) dbo.TableName) SELECT id, device ID, sensor ID, value from CTE where the ID
This is a group Ochc returns row with the ID if you want a different result you ORDER need to change BY ID DESC
Demo: (Your results)
No comments:
Post a Comment