I need to find the record for the last time each person logged in. This would work with T-SQL
SELECT *
FROM
(
SELECT lh.*, ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY DateCreated DESC) AS RowNumber
FROM LoginHistory lh
) lhp
where lhp.RowNumber = 1
But due to DocumentDb's reduced feature set, I can't figure out how to handle this.
I assume this would need to be solved as a Stored Procedure, but I can't figure how to structure that either. Loop over multiple async calls? (Not sure how that would even be done.) Download all the records and filter them just using JS?
How can I translate this?
UPDATE: Sample output
PARTITION BY is similar to a GROUP BY, but instead of aggregating the results, it treats the records as a kind of scope. So
SELECT lh.*, ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY DateCreated DESC) AS RowNumber
FROM LoginHistory lh
would return something like
UserId DateCreated RowNumber
1 2015-12-10 22:44:03 1
1 2015-12-10 13:35:12 2
1 2015-12-09 18:52:25 3
2 2015-12-10 20:53:13 1
2 2015-12-10 08:12:41 2
It basically says, "For a given UserId, order those records by DateCreated".
Then I just select RowNumber = 1 in the outer query and I have the latest record for each user.
My question used the SQL 2015+ syntax, but it is also possible with pre-SQL 2005 syntax, which would be done with something like this:
Select Date, User, Status, Notes
from [SOMETABLE]
inner join
(
Select max(Date) as LatestDate, [User]
from [SOMETABLE]
Group by User
) SubMax
on [SOMETABLE].Date = SubMax.LatestDate
and [SOMETABLE].User = SubMax.User
Unfortunately, DocumentDb doesn't support GroupBy either.