MAX函数不返回最大值,而是返回所有值

时间:2017-08-07 03:21:07

标签: sql sql-server

我有一个User_Transfer表,如下所示

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条 我试图获得如下所示的结果:

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

4 个答案:

答案 0 :(得分:1)

不要将ID列放在组中这就是为什么你得到所有行, 所以请尝试以下查询

 SELECT X.USER_ID,
       X.USER_NO,
       EB.USER_NAME,
       EB.NATIONALITY_CODE,
       x.DATE_FROM,
       X.DATE_TO
 FROM
 (SELECT USER_ID,USER_NO,Max(DATE_From) AS DATE_From, MAX(DATE_TO) AS DATE_TO
    FROM USER_TRANSFER
    GROUP BY USER_ID,USER_NO) X
 left join USER_ID EB on x.USER_ID=EB.USER_ID

 ORDER BY X.USER_NO

答案 1 :(得分:0)

您可以参考我的解决方案:

SELECT     Uf.USER_ID,
           Uf.USER_NO,
           Upro.USER_NAME,
           Upro.NATIONALITY_CODE,
           Uf.DATE_FROM,
           T1.DATE_TO
   FROM User_Transfer as Uf
JOIN (Select USER_ID,
       USER_NO,
       MAX(Date_to) AS Date_To
       FROM User_Transfer 
       GROUP BY USER_ID, USER_NO) as T1 --get all user_id and user_no that have max date_to
ON T1.USER_ID = Uf.USER_ID AND T1.USER_NO = Uf.USER_NO 
                           AND T1.Date_To = Uf.Date_To
JOIN User_Profile AS Upro
ON Upro.USER_ID = Uf.USER_ID

答案 2 :(得分:0)

试一试:

http://www.sqlfiddle.com/#!9/508ae6/27

select tmp1.User_ID,tmp1.User_No,User_Profile.User_Name,User_Profile.Nationality_Code ,
tmp1.Date_From,tmp1.Date_To
from 
(
    select * from User_Transfer U
    order by Date_From desc
) as tmp1,User_Profile
 where
 tmp1.User_ID=User_Profile.User_ID
 group by User_ID 

答案 3 :(得分:0)

尝试以下查询

   with temp as 
   (
    select t.user_id,
    t.user_no,
    p.user_name,
    p.nationality_code, 
   max(Date_to) as Date_to
   from user_transfer t inner join user_profile p 
   on t.user_id=p.user_id
   group by t.user_id,t.user_no,p.user_name,p.nationality_code 
 )
select 
  a.user_id,a.user_no,p.user_name,p.nationality_code,a.date_from,p.date_to 
  from user_transfer a join temp p 
  on a.user_id=p.user_id
  and a.date_to=p.date_to;