Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.databases.ms-sqlserver    |    Notorious Rube Goldberg contraption    |    19,505 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 17,809 of 19,505    |
|    Erland Sommarskog to soni kundani    |
|    Re: help with select    |
|    02 Jun 10 23:34:29    |
      dcbbc497       From: esquel@sommarskog.se              soni kundani (soni.kundani@gmail.com) writes:       > I have the following table:       > TableName: WC       > Columns:       > UserID PeriodOfEntry LastName       > 1 06/02/2010 Apple       > 1 04/01/2009 Apple       > 2 04/12/2000 Peach       > 2 04/11/2000 Peach       > 2 06/02/2010 Peach       > 3 01/11/2000 Grape       >       > I'm trying to get it so the query will return a unique UserID with       > it's latest PeriodOfEntry only,              WITH numbered AS (        SELECT UserID, PeriodOfEntry, LastName,        rowno = row_number() OVER(PARTITION BY UserID        ORDER BY PeriofOfEntry DESC)        FROM tbl       )       SELECT UserId, PeriodOfEntry, LastName       FROM tbl       WHERE rowno = 1              This solution requires SQL 2005 or alter. (Please always include which       version of SQL Server you use.)              The part that starts with WITH is a Common Table Expression, which       can be described as a temporary view, visible only for the query.       In the CTE we determine a row number which we then filter on.              --       Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se              Links for SQL Server Books Online:       SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx       SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx       SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca