Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:01:30+00:00 2026-06-02T13:01:30+00:00

I have a table with this data: ID voting_ID username timestamp XMLBallot 1 9

  • 0

I have a table with this data:

ID   voting_ID   username   timestamp              XMLBallot
1    9           voter01    23. 4. 2012 8:54:45    xmldata
2    9           voter01    21. 4. 2012 14:00:34   xmldata
3    9           voter02    20. 4. 2012 16:01:10   xmldata
4    11          voter01    23. 4. 2012 8:40:45    xmldata
5    9           voter03    19. 4. 2012 21:18:49   xmldata

I need to get only one newest ballot for each voter (username) in specific voting_ID.

For example data I need to be returned for @voting_ID=9

ID   voting_ID   username   timestamp              XMLBallot
1    9           voter01    23. 4. 2012 8:54:45    xmldata
3    9           voter02    20. 4. 2012 16:01:10   xmldata
5    9           voter03    19. 4. 2012 21:18:49   xmldata

Please help me build that SQL Server 2008 query.. thank you
PS: table name is ballots

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:01:33+00:00Added an answer on June 2, 2026 at 1:01 pm

    You have several options here but using adding a ROW_NUMBER grouped by user and sorted (descending) on your timestamp allows you to easily select the latest records.

    Using ROW_NUMBER

    SELECT *
    FROM   (
             SELECT ID, voting_ID, username, timestamp, XMLBallot
                    , rn = ROW_NUMBER() OVER (PARTITION BY voting_ID, username ORDER BY timestamp DESC)
             FROM   Ballots
           ) bt 
    WHERE  rn = 1
    

    Alternatively, you can select the maximum timestamp per user and join on that.

    Using MAX

    SELECT bt.ID, bt.voting_ID, bt.username, bt.timestamp, bt.XMLBallot
    FROM   Ballots bt
           INNER JOIN (
              SELECT username, voting_ID, timestamp = MAX(timestamp)
              FROM   Ballots
              GROUP BY
                     username, voting_ID
            ) btm ON btm.username = bt.Username
                     AND btm.voting_ID = bt.voting_ID
                     AND btm.timestamp = bt.timestamp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this table schema. ID AccNo Amount Say I have this data
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
I have this data in a table, for instance, id name parent parent_id 1
I have this JSF table which is used to display data. I want to
If I have a table column with data and create an index on this
I have no control over how the data is saved in this table. However,
I like to have a query like this: select data from table where (x
I'm a bit confused on this. I have a data table structured like this:
I have a table with this data: Id Qty -- --- A 1 A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.