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 9227305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:02:30+00:00 2026-06-18T05:02:30+00:00

SQL Fiddle: http://sqlfiddle.com/#!3/9b459/6 I have a table containing answers to the question Will you

  • 0

SQL Fiddle: http://sqlfiddle.com/#!3/9b459/6

I have a table containing answers to the question “Will you attend this event?”. Each user might respond several times and all answers are stored in the table. Normally we’re only interested in the latest answer, and I’m trying to construct an efficient query for that. I’m using SQL Server 2008 R2.

Table contents for one event:

Table contents

Column types: int, int, datetime, bit
Primary key: (EventId, MemberId, Timestamp)

Note that Member 18 first answered No and later Yes, member 20 answered Yes at first and later No, member 11 answered No and later No again. I would like to filter out these member’s first answers. Also, there might be more than one answer that should be filtered – a user might for example answer Yes, Yes, No, Yes, No, No, No.

I have tried a few different ideas, and have evaluated them in SQL Server Management Studio by entering all queries, selecting Display Estimated Execution Plan and comparing each query’s total cost in percent. Is that a good method for evaluating the performance?

The different queries tested so far:

-----------------------------------------------------------------
-- Subquery to select Answer (does not include Timestamp)
-- Cost: 63 %
-----------------------------------------------------------------
select distinct a.EventId, a.MemberId,
(
  select top 1 Answer
  from    Attendees
  where EventId   = a.EventId
  and   MemberId  = a.MemberId
  order by Timestamp desc
) as Answer
from    Attendees a
where a.EventId = 68

-----------------------------------------------------------------
-- Where with subquery to find max(Timestamp)
-- Cost: 13 %
-----------------------------------------------------------------
select a.EventId, a.MemberId, a.Timestamp, a.Answer
from     Attendees a
where  a.EventId = 68
and    a.Timestamp =
(
  select max(Timestamp)
  from     Attendees
  where  EventId  = a.EventId
  and    MemberId = a.MemberId
)
order by a.TimeStamp;

-----------------------------------------------------------------
-- Group by to find max(Timestamp)
-- Subquery to select Answer matching max(Timestamp)
-- Cost: 23 %
-----------------------------------------------------------------
select a.EventId, a.MemberId, max(a.Timestamp),
(
  select top 1 Answer
  from    Attendees
  where EventId   = a.EventId
  and   MemberId  = a.MemberId
  and   Timestamp = max(a.Timestamp)
) as Answer
from    Attendees a
where a.EventId = 68
group by a.EventId, a.MemberId
order by max(a.TimeStamp);

It would be nice to avoid using a subquery for each member. In the last query I tried using group by but still had to use a subquery for the Answer column. I would really like something like this, but that isn’t valid SQL of course:

select a.EventId, a.MemberId, max(a.Timestamp), a.Answer <-- Picked from the line selected by max(a.Timestamp)
from  Attendees a
where a.EventId = 68
group by a.EventId, a.MemberId
order by max(a.TimeStamp);

Any other ideas for an efficient query?


EDIT:

Very impressed by SQL Fiddle, I have entered my actual data there now:
http://sqlfiddle.com/#!3/9b459/6

  • 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-18T05:02:31+00:00Added an answer on June 18, 2026 at 5:02 am

    I prefer the CTE approach as well, but here is another option using a subquery that should work:

    SELECT T.EventId, T.MemberId, T.TimeStamp, T.Answer
    FROM TableName T
     JOIN (
       SELECT EventId, MemberId, Max(Timestamp) MaxTimeStamp
       FROM TableName
       GROUP BY EventId, MemberId ) T2 ON T.EventId = T2.EventId 
        AND T.MemberId = T2.MemberId 
        AND T.TimeStamp = T2.MaxTimeStamp
    

    With that said, I imagine the CTE would have a better performance.

    EDIT — Not sure about performance any longer — here is the SQL Fiddle for both — you can see the execution plan for each.

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following in my sql fiddle: http://sqlfiddle.com/#!2/62429/2 I have a username , which i need
I have an sql fiddle here: http://www.sqlfiddle.com/#!2/fbd48/1 The colors should be red for flower
Check this example before reading the question - http://www.sqlfiddle.com/#!2/fcf3e/8 The following data comes from
Take a look at this fiddle: http://sqlfiddle.com/#!6/18324/2 Expand the very first execution plan, for
I have this database setup: http://sqlfiddle.com/#!12/b2989 I want to add a record called solrID
Looking at the following SQL fiddle: http://sqlfiddle.com/#!2/962496/1 How could one select all orders from
I already provide sql fiddle with schema and sample data. http://sqlfiddle.com/#!2/e9d22/7/0 If I would
Following is my SQL fiddle: http://sqlfiddle.com/#!2/f9bae/1 In which i am trying to check if
I have some data in a format described in this sqlfilddle: http://sqlfiddle.com/#!4/b9cdf/2 Basically, a
This sqlfiddle link seems to retain state across invocations: http://sqlfiddle.com/#!2/125bc/1 It contains this schema

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.