Ms Sql Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbarnes2231
    New Member
    • Dec 2005
    • 1

    Ms Sql Query

    I am trying to write a query that will choose a row without duplicates. The table is used as a checking in/out log and I need to query the latest check in or out. There is a column that is in date-time format that records the time of the check in or out, so inside of my query, how can I grab all of the distinct rows, choosing the latest check in or out transaction for each user?
  • mllin1234
    New Member
    • Oct 2006
    • 2

    #2
    Originally posted by mbarnes2231
    I am trying to write a query that will choose a row without duplicates. The table is used as a checking in/out log and I need to query the latest check in or out. There is a column that is in date-time format that records the time of the check in or out, so inside of my query, how can I grab all of the distinct rows, choosing the latest check in or out transaction for each user?
    Please try this in SQL 2005

    WITH [TEMPTABLENAME] AS
    (SELECT ROW_NUMBER() OVER (PARTITION BY user ORDER BY loginout_dateti me desc) AS ROWID, * FROM logtablename)
    SELECT user,loginout_d atetime FROM [TEMPTABLENAME] WHERE ROWID=1

    Comment

    Working...