2012-07-10 80 views
0

我有兩張表(serverlogdetail a,serverlogid b),用於處理用戶活動 用戶可以在不同的應用程序中使用不同的日誌(a.entrydate) 對於所有到一個用戶具有訪問權限的應用程序,我需要與上次他/她登錄返回一個列表。SQL與MAX一起加入

SELECT b.appname ,max(to_char(a.entrydate)), a.username, a.msgnumber 
    FROM serverlogdetail a, serverlogid b 

    WHERE a.msgnumber = 1020055 
    AND A.entrydate BETWEEN (SYSDATE-90) AND SYSDATE 

GROUP BY b.appname, a.dbname, a.username,a.msgnumber, a.entrydate 
ORDER BY b.appname, max(to_char(a.entrydate)) 

在此先感謝

+0

你需要的結果msgnumber ? – Chandu 2012-07-10 23:17:32

+0

沒有「a」和「b」的「連接」 - 它將選擇「b」中的所有行並與從「a」中選擇的記錄相乘 – alfasin 2012-07-10 23:24:12

回答

0
SELECT b.appname , a.username, a.msgnumber, max(to_char(a.entrydate)) 
FROM serverlogdetail a, serverlogid b 

WHERE a.msgnumber = 1020055 
AND A.entrydate BETWEEN (SYSDATE-90) AND SYSDATE 
AND A.something = B.something   -- the condition you're missing!!! 

GROUP BY b.appname, a.username, a.msgnumber 
ORDER BY b.appname, max(to_char(a.entrydate))