SQL Server Performance

T-SQL Query

Discussion in 'T-SQL Performance Tuning for Developers' started by SQL2000DBA, Oct 15, 2009.

  1. SQL2000DBA New Member

    Hi,
    I need small help in writing T-SQL statement in SQL Server 2000. I have table with following records
    S_No MYNAME MYDATE
    1 A 2009-10-15 15:11:40.680
    2 A 2009-11-15 15:11:40.680
    3 A 2009-09-15 15:11:40.680
    4 A 2009-10-15 12:11:40.680
    5 A 2009-11-15 15:12:40.680
    I need to write T-SQL in such a way that I get following records

    3 A 2009-09-15 15:11:40.680
    1 A 2009-10-15 15:11:40.680
    5 A 2009-11-15 15:12:40.680
    means for everymonth i need to get the latest row
  2. SQL2000DBA New Member

    Thanks. I got the answer asSELECT * FROM MYTABLE T INNER JOIN
    (
    SELECT MAX(MYDATE) AS MYDATE,DATEPART(MM,MYDATE) AS MYMONTH FROM MYTABLE GROUP BY DATEPART(MM,MYDATE)) AS MYON
    T.MYDATE=MY.MYDATE

Share This Page