2016-03-02 58 views
0

我有一個表像如何爲此SQL查詢編寫左外連接?

ParsereplyId Message Callid status EMailid  subject 

------------------------------------------------------------------------ 
     1  hi   2222 Replied [email protected] Log a cll 
     2  hello  2222 Replied [email protected] Re: 
     3  hi2  2222 New  [email protected] Re:log a cll 
     4  hello2  2223 Read  [email protected]  Log a cldf 
     5  how r u 2223 New  [email protected]  Re:Log a 

從上面的表格我想下面的輸出:

ParsereplyId Message Callid status EMailid  subject 

------------------------------------------------------------------- 
     3  hi2  2222 New  [email protected] Re:log a cll 
     5  how r u 2223 New  [email protected]  Re:Log a 

我曾嘗試下面的查詢。 但我想做到在LEFT OUTER JOIN

SELECT A.[ParsedReplyId] 
       , A.[EMailId] 
       , A.[Message] 
       , A.[CallId] 
       , [UM_User].[UserName]     
FROM IM_IncidentReplyMail AS A 
     INNER JOIN 
       (SELECT MAX(ParsedReplyId) AS parseid 
       FROM [IM_IncidentReplyMail] 
       GROUP BY [CallId]) AS B 
     INNER JOIN 
     [UM_User] ON [UM_User].[EmailId] = A.[EmailId] 
     WHERE 
      B.parseid = A.[ParsedReplyId] 

如何實現上面的SQL查詢內部聯接?

編輯:我糾正了我的錯誤上面

UM_USer表包含​​3210,基於電子郵件ID從兩個表中我能得到該郵件的用戶名。

編輯2: 我可以使用左外連接,而不是使用內部聯接像

SELECT A.[ParsedReplyId] 
        , A.[EMailId] 
        , A.[Message] 
        , A.[CallId] 
        , [UM_User].[UserName]     
      FROM IM_IncidentReplyMail AS A 
      LEFT OUTER JOIN 
        (SELECT MAX(ParsedReplyId) AS parseid 
        FROM [IM_IncidentReplyMail] 
        GROUP BY [CallId]) AS B 
      ON 1=1 
      LEFT OUTER JOIN 
      [UM_User] ON [UM_User].[EmailId] = A.[EmailId] 
      WHERE 
       B.parseid = A.[ParsedReplyId] 

在這裏,我使用1 = 1個條件這是不是在查詢中使用的正確方式。它使我的代碼變得醜陋。所以我要求你幫忙解決這個問題。

+0

你知道你錯過了'INNER'和'JOIN'權之間的空間修飾的查詢? – Coderchu

+0

正確地給出表格的詳細信息... – Avi

+0

我假設這是您正在使用的SQL Server,是否正確?你想爲每個CallID組使用ParsedReplyID最高的記錄嗎?如果是這樣,你可以從內部聯接中選擇*從表名中選擇*(選擇max(parsedreplyid)作爲mp,callid從表名組中通過callid)b對a.parsedreplyid = b.mp' – zedfoxus

回答

1

我已經按照你需要的輸出

DECLARE @Table1 TABLE 
     (Id INT, Message VARCHAR(7), Call INT, status VARCHAR(7), EMail VARCHAR(11), subject VARCHAR(12)) 
    ; 

    INSERT INTO @Table1 
     (Id, Message, Call, status, EMail, subject) 
    VALUES 
     (1, 'hi', 2222, 'Replied', '[email protected]', 'Log a cll'), 
     (2, 'hello', 2222, 'Replied', '[email protected]', 'Re:'), 
     (3, 'hi2', 2222, 'New', '[email protected]', 'Re:log a cll'), 
     (4, 'hello2', 2223, 'Read', '[email protected]', 'Log a cldf'), 
     (5, 'how r u', 2223, 'New', '[email protected]', 'Re:Log a') 
    ; 

     SELECT T.Id, 
     TT.Message, 
     TT.Call, 
     TT.status, 
     TT.EMail, 
     TT.subject 
     FROM @Table1 tt 
     LEFT OUTER JOIN 
     (
     SELECT Id, 
     MAX(subject) OVER(PARTITION BY call ORDER BY call) subject, 
     MAX(Message) OVER(PARTITION BY call ORDER BY call) Message FROM @Table1)T 
     ON T.Id = TT.Id AND T.Message = TT.Message AND T.subject = TT.subject 
WHERE T.id IS NOT NULL 
+0

我們不能用簡單的'ROW_NUMBER'實現這個嗎 –

+0

@ mohan111:你的查詢很有用,但我明確表示我想在左邊的外部實現它加入,而不是在內部加入。無論如何抱歉沒有及時回覆。 – Gomathipriya

+0

是的,我們可以在簡單的row_number中做,但兩者都會達到相同的目的,因爲分區將通過調用列來完成@MotoGP – mohan111