In my project I use:
- F# with query workflow
- Entity Framework Core 3.11
- MS SQL
I have a problem similar to this one: SQL select only rows with max value on a column, but I'm wondering how to express the SQL presented in that question using F# query workflow:
SELECT a.id, a.rev, a.contents
FROM YourTable a
INNER JOIN (
SELECT id, MAX(rev) rev
FROM YourTable
GROUP BY id
) b ON a.id = b.id AND a.rev = b.rev
Direct translation
For a direct translation, the inner query is:
And then, in theory, I think the full query should be:
However, that doesn't work:
I could be wrong, or it could be a bug/limitation. Perhaps someone has a workaround.
Alternate translation
However, if you'll accept a slightly different translation, it does work:
The generated SQL is:
Output is:
Note that I had to set a composite primary key of
id, revwhen building the model:Full code is here.