2011-11-17 67 views
1

我试过各种不同的方式来查询这个,但似乎无法得到正是我想要的...我以下使用的工作伟大与1例外。 ..我需要查询只返回1行PER UNIQUE ID。底部是我期待得到的结果的一个例子。SQL在表中每个唯一ID只返回1行

我当前的查询:

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. 

+0

是created_timestamp保证是每ID唯一?如果不是你想如何处理关系?如果您希望多个记录返回关系,请使用galador。当你想任意挑选一个时,请使用Alex的答案。 –

回答

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 
+0

这和我期望的完全一样。谢谢! 哇......有一点我很接近这个相同的东西......我会在某个时候得到它。 ;) –

+0

哎呀!忘了补充一下,我需要改变MIN(到MAX(;) –

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最古老创建的记录。

+0

是的,在这一点上,我不知道我在想什么,使用created_by_user那里...:/ –

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 
+0

而且绝对检查where子句,看起来不像你想要的。 – Alex

+0

这一个返回错误... Msg 156,Level 15,State 1,Line 11 关键字'WHERE'附近的语法不正确。 –

+0

糟糕,修正了语法错误。 – Alex

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 :-)

+0

如果你在最上面改变MIN到MAX,这也可以工作;;)目前它给了我第一篇文章,我想要最后一篇文章。 (最早的时间戳) –

相关问题