我有User_Transfer的表看起來像下面MAX函數沒有返回最大值,返回的所有值,而不是
ID User_ID User_No Date_From Date_To
1 00001 KJH789 2014-04-26 2014-04-29
2 00001 KJH789 2014-04-01 2014-04-04
3 00004 TGI567 2014-04-26 2014-04-29
4 00026 RTW763 2014-04-09 2014-04-12
和表User_Profile
ID User_ID User_Name Nationality_Code
1 00001 John JAP
2 00004 Sarah SG
3 00026 Timmy SG
我的查詢:
SELECT X.USER_ID,
X.USER_NO,
X.USER_NAME,
X.NATIONALITY_CODE,
U.DATE_FROM,
X.DATE_TO
FROM
(SELECT
ID,
USER_ID,
USER_NO,
(SELECT EB.USER_NAME FROM USER_PROFILE EB WHERE EB.USER_ID = USER_TRANSFER.USER_ID) AS USER_NAME,
(SELECT EB.NATIONALITY_CODE FROM USER_PROFILE EB WHERE EB.USER_ID = USER_TRANSFER.USER_ID) AS NATIONALITY_CODE
MAX(DATE_TO) AS DATE_TO
FROM USER_TRANSFER
GROUP BY
ID,
USER_ID,
USER_NO,
EMPLOYEE_NO) X,
USER_TRANSFER U
WHERE
U.ID = X.ID AND
U.USER_ID = X.USER_ID
ORDER BY X.USER_NO
我上面的查詢返回了4條記錄,而不是3條。
我試過t Ø得到看起來像下面的結果:
User_ID User_No User_Name Nationality_Code Date_From Date_To
00001 KJH789 John JAP 2014-04-26 2014-04-29
00004 TGI567 Sarah SG 2014-04-26 2014-04-29
00026 RTW763 Timmy SG 2014-04-09 2014-04-12
什麼是你想怎麼辦?請詳細解釋 – asmgx
很驚訝沒有人試過使用'Row_number'窗口函數 –