2011-11-17 98 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,這也可以工作;;)目前它給了我第一篇文章,我想要最後一篇文章。 (最早的時間戳) –

相關問題