![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
#4
| |||
| |||
|
|
JOIN (SELECT RouterId, DeviceId, MIN(ABS(C.Metric - Metric)) AS min_metric |
#5
| |||
| |||
|
|
unfortunately it doesn't work as expected. SQL CE says, it can't find column C.Metric SELECT A.RouterId, A.DeviceId, A.Metric FROM LinkTable AS A JOIN DeviceTable AS C ON (C.Id = A.DeviceId AND (C.MacAddress = 0x1234567890ab OR C.MacAddress = ... OR ...)) JOIN (SELECT RouterId, DeviceId, MIN(ABS(C.Metric - Metric)) AS min_metric FROM LinkTable GROUP BY RouterId, DeviceId) AS B ON B.DeviceId = A.DeviceId AND B.RouterId = A.RouterId AND ABS(C.Metric - A.Metric) = min_metric; |
#6
| |||
| |||
|
|
Note, by the way, that the query can return more than one row for the same (RouterId, DeviceID), for instance if you have one tuple with Metric = 95 and one with Metric = 105. |
|
And important property of a derived table is that it is blind for tables outside it, why you cannot refer to DeviceTable it. Keep in mind, that logically the derived table is computed first. |

|
Looking at your query it seems to me that you need the DeviceTable in two places: SELECT LT1.RouterId, LT1.DeviceId, LT1.Metric [...] |

#7
| |||
| |||
|
|
OK, but now the Question is (if this would work in Compact Edition), would it make sense to make such a complex query? May be it is better to put some logic outside of the query into the main application? |
#8
| |||
| |||
|
|
But had you been working in "real" SQL Server, my answer would have been "probably not". (If you think that is a complex query, you should see some of those I write at work. :-) |
No I didn't work in "real" SQL Servers so far, only
Nevertheless work had to
#9
| |||
| |||
|
|
OK, but I think, it is better to call that query for each of the MAC addresses on my list instead to create a real huge query (if this would be possible). |
![]() |
| Thread Tools | |
| Display Modes | |
| |