查询中的SQL MAX问题

时间:2014-07-22 16:19:09

标签: sql casting max

谁能告诉我为什么这不起作用?它给了我所有的访问日期,我只是在寻找最近的访问日期:

SELECT ID , MAX(CAST((CAST(VISITMO  AS nvarchar(2)) + '/' +
                      CAST(VISITDAY AS nvarchar(2)) + '/' +
                      CAST(VISITYR  AS nvarchar(4))) AS date)
                ) AS LastVisitDATE
  FROM [VisitTable] 
  WHERE ID IN (SELECT ID FROM Table2 WHERE other criteria is met)

GROUP BY ID, VISITYR, VISITMO, VISITDAY

由于我遇到的错误,我一直在添加GROUP BY语句

1 个答案:

答案 0 :(得分:1)

SELECT ID , MAX(CAST((CAST(VISITMO  AS nvarchar(2)) + '/' +
                      CAST(VISITDAY AS nvarchar(2)) + '/' +
                      CAST(VISITYR  AS nvarchar(4))) AS date)
                ) AS LastVisitDATE
  FROM [VisitTable] 
GROUP BY ID