title: Select TOP 20 Distinct Rows in Query
body: <p>I keep a log of User Actions in a table with a date stamp.</p>
  
  <p>Each time a user goes into a project, it logs that they viewed that project and stamps the datetime.</p>
  
  <p>I want to get the TOP 20 Projects that the user has last gone into.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>SELECT DISTINCT TOP (20) Users_Tran_ID
  FROM App_UserLog
  WHERE (Users_ID = 'ADMIN') AND (Users_Transaction LIKE 'Permit Viewed%')
  ORDER BY App_UserLog.Users_LogDate DESC
  </code></pre>
  
  <p>The problem is, I don't want the Users_LogDate in my output, as I need UNIQUE Users_Tran_ID.</p>
  
  <p>When I remove the Users_LogDate from the SELECT I get the following error:</p>
  
  <pre><code>ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
  </code></pre>
  
  <p>Here is the basic select and result set I get.  I just want the distinct Users_Tran_ID and only the TOP 20.</p>
  
  <pre><code>ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:35:04.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:29:09.000
  ADMIN   Permit Viewed   B2006-0043  2013-08-29 17:29:05.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:28:49.000
  ADMIN   Permit Viewed   B2008-0167  2013-08-29 17:17:13.000
  ADMIN   Permit Viewed   B2006-0043  2013-08-29 17:17:08.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:16:55.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:16:45.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:16:38.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 17:16:32.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 11:39:59.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 11:22:05.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 11:22:03.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:23:17.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:21:58.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:20:41.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:20:37.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 10:18:01.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 10:17:58.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:17:54.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:01:50.000
  ADMIN   Permit Viewed   B2011-0012  2013-08-29 10:01:48.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 09:56:32.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 09:53:31.000
  ADMIN   Permit Viewed   B2007-0478  2013-08-29 09:53:27.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-15 12:51:09.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-15 12:51:07.000
  ADMIN   Permit Viewed   BLD2012-0094    2013-08-13 12:01:39.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:34.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:28.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:24.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:21.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:13.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:09.000
  ADMIN   Permit Viewed   ENG2013-0047    2013-08-05 10:40:07.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-05 10:37:55.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-05 10:37:50.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-05 10:37:46.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-05 10:37:40.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-05 10:37:33.000
  ADMIN   Permit Viewed   ENG2012-0141    2013-08-05 10:37:31.000
  ADMIN   Permit Viewed   BLD2012-0919    2013-08-05 10:27:21.000
  ADMIN   Permit Viewed   BLD2012-0919    2013-08-05 10:27:17.000
  ADMIN   Permit Viewed   ENG2012-0119    2013-08-05 10:26:51.000
  ADMIN   Permit Viewed   ENG2012-0119    2013-08-05 10:26:48.000
  </code></pre>
  
  <p>How can I do this?</p>