0

我有很多用户,每个人回答 10 个问题并留下一些时间戳。我理想地想要做的是找到第 n 个用户的完整时间,其中 n 由时间戳的升序决定,而不是用户 ID。

这行不通,但是为了让您了解我需要什么,如果可以运行,我将非常高兴:

SELECT <N-TH> MAX(u.time) AS maxTime FROM Users u
Group BY u.userId   ORDER BY maxTime

有什么想法吗?现在我正在跑步

SELECT MAX(u.time) AS maxTime FROM Users u
Group BY u.userId   ORDER BY maxTime

将结果作为数组获取,然后找到第 n 个条目。

4

4 回答 4

0

许多可能的方法,但只是袖手旁观......可以使用 CTE 或子查询。尝试

SELECT MAX([maxTime]) AS [LastEntry] FROM (
    SELECT MAX(u.time) AS maxTime,u.userId 
    FROM Users u 
    GROUP BY u.userId,u.time) t

好的,试试这个...

WITH CTE ([maxTime], [userId ]) AS (
    SELECT MAX([time]) AS [maxTime],
        [userId]
    FROM Users
    GROUP BY [userId])
    SELECT MAX([maxTime]) AS [maxTime]
    FROM CTE;

高温高压

戴夫

于 2013-06-07T13:35:00.960 回答
0

您是否尝试过使用ROW_NUMBER()功能

SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY maxTime) as RowNum, MAX(u.time) AS maxTime FROM Users u
Group BY u.userId
) A
WHERE RowNum = NthNumber
ORDER BY maxTime

NthNumber排名数字在哪里。

于 2013-06-07T13:38:39.080 回答
0

GROUP BYUserId,并ROW_NUMBER根据应用max(u.time)创建排名伪列,然后您可以对其进行过滤

select min_utime, max_utime, userId from (
select min(u.time) min_utime, max(u.time) max_utime, u.userId,
row_number() over (order by max(u.time) asc) as ranker
from Users u
group by u.userId
) Z  where ranker  = @n
于 2013-06-07T13:44:53.837 回答
0

获取按最大时间排序的前 10 行:

SELECT TOP 10 userId, MAX(time) AS maxtime
FROM Users
GROUP BY userId
ORDER BY maxtime ASC

然后只需颠倒顺序并占据第一行:

SELECT TOP 1 *
FROM (
  SELECT TOP 10 userId, MAX(time) AS maxtime
  FROM Users
  GROUP BY userId
  ORDER BY maxtime ASC
) s
ORDER BY maxtime DESC;
于 2013-06-09T13:09:00.790 回答