SQL在表中每个唯一ID只返回1行

时间:2011-11-17 19:14:34

标签: sql sql-server sql-server-2008

我已经尝试了各种不同的方式来查询这个但是似乎无法得到我想要的...我在下面使用的工作很有用1例外...我需要让查询只返回1行PER UNIQUE ID。在底部是我最终想要得到的一个例子。

我当前的查询:

SELECT
  T.id,
  T.request_type,
  T.created_timestamp,
  T.status,
  T.subject,
  P.id,
  P.created_timestamp,
  P.created_by_user,
  P.post
FROM request_threads T
INNER JOIN request_posts P
  ON T.id = P.id
WHERE T.created_by_user = 2
  AND P.created_by_user != (
    SELECT MAX(P2.created_timestamp)
    FROM request_posts P2 WHERE id = T.id
    )
ORDER BY P.created_timestamp DESC

我当前的查询结果:

id  request_type    created_timestamp   status  subject id  created_timestamp   created_by_user post
4   7   2011-11-08 10:50:57.440 7   Request 4 blah black    4   2011-11-14 17:44:16.603 3   Changed status to Closed.
4   7   2011-11-08 10:50:57.440 7   Request 4 blah black    4   2011-11-14 17:44:07.060 3   Test for caps and no punct!
4   7   2011-11-08 10:50:57.440 7   Request 4 blah black    4   2011-11-14 17:43:36.797 3   New formated post with a capital first letter and a period at the end.
4   7   2011-11-08 10:50:57.440 7   Request 4 blah black    4   2011-11-14 13:42:27.707 3   Changed status to Pending.  Just testing...      test again      Blah
2   7   2011-11-07 14:53:01.410 7   Request 2 blah green    2   2011-11-08 13:05:23.183 3   Changed status to Closed.
4   7   2011-11-08 10:50:57.440 7   Request 4 blah black    4   2011-11-08 10:50:57.527 2   This is the 1st yellow post for four
2   7   2011-11-07 14:53:01.410 7   Request 2 blah green    2   2011-11-07 14:53:01.420 2   This is the 1st green post for two

我希望得到的结果:
(注意我想要request_posts表中最早的created_timestamp)

id  request_type    created_timestamp   status  subject id  created_timestamp   created_by_user post
4   7   2011-11-08 10:50:57.440 7   Request 4 blah black    4   2011-11-14 17:44:16.603 3   Changed status to Closed.
2   7   2011-11-07 14:53:01.410 7   Request 2 blah green    2   2011-11-08 13:05:23.183 3   Changed status to Closed.

4 个答案:

答案 0 :(得分:1)

select *
FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY p.created_timestamp DESC) AS row
        , T.id AS tID, T.request_type, T.created_timestamp AS c_timestamp, T.status, T.subject
        , P.id AS pID, P.created_timestamp AS p_timestamp, P.created_by_user, P.post
    FROM request_threads T 
    JOIN request_posts P
        ON T.id = P.id
    WHERE T.created_by_user = 2
    ) x
WHERE row = 1

答案 1 :(得分:1)

你说你想要帖子表中最老的一行,所以你应该能够通过稍微改变你的WHERE子句来获得你想要的结果:

SELECT 
    T.id,
    T.request_type,
    T.created_timestamp,
    T.status,
    T.subject,
    P.id,
    P.created_timestamp,
    P.created_by_user,
    P.post
FROM request_threads T
INNER JOIN request_posts P
    ON T.id = P.id
WHERE T.created_by_user = 2
  AND P.created_timestamp = 
        (SELECT MIN(P2.created_timestamp)
         FROM request_posts P2
         WHERE id = T.id)
ORDER BY P.created_timestamp DESC

答案 2 :(得分:0)

我不明白你的where子句的这一部分:

AND P.created_by_user != (SELECT MAX(P2.created_timestamp) FROM request_posts P2 WHERE id = T.id)

您正在将用户ID与日期进行比较。 我想你可能想用这个替换这个句子:

AND P.created_timestamp = (SELECT MIN(P2.created_timestamp) FROM request_posts P2 WHERE id = T.id)

通过这种方式,您可以为每个 id 值获取request_posts中最早创建的记录。

答案 3 :(得分:0)

一种方法是在创建的时间戳上添加一个子选择,以确保获得单个记录。

SELECT T.id, T.request_type, T.created_timestamp, T.status, T.subject, P.id, P.created_timestamp, P.created_by_user, P.post
FROM request_threads T 
    INNER JOIN request_posts P
        ON T.id = P.id
        AND P.created_timestamp = (SELECT MIN(S.created_timestamp) FROM request_posts S WHERE P.id = S.id)
WHERE
T.created_by_user = 2
    AND P.created_by_user != (SELECT MAX(P2.created_timestamp) FROM request_posts P2 WHERE id = T.id)
ORDER BY P.created_timestamp DESC

编辑为最早= MIN: - )