SQL Max(date) without group by

旧时模样 提交于 2020-12-26 07:54:56

问题


I have the following table...

MemberID  ServDate
001       12-12-2015
001       12-13-2015
001       12-15-2015
002       11-30-2015
002       12-04-2015

And I want to make it look like this...

MemberID  ServDate     LastServDate
001       12-12-2015   12-15-2015
001       12-13-2015   12-15-2015
001       12-15-2015   12-15-2015
002       11-30-2015   12-04-2015
002       12-04-2015   12-04-2015

Is there a way I can do this without having to use a GROUP BY or nested query? (I'm dealing with a very large database and the GROUP BY slows things down considerably)


回答1:


SELECT 
  MemberID, ServDate, 
  MAX(ServDate) OVER (PARTITION BY MemberID) AS LastServDate
FROM Table

Standard SQL, so works in most modern RDBMS (including SQL Server and Oracle).

EDIT by the way, if you want to learn more: MSDN ref. for OVER



来源:https://stackoverflow.com/questions/34094638/sql-maxdate-without-group-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!