View Poll Results: Was this post easy to understand

Voters
2. You may not vote on this poll
  • Yes

    1 50.00%
  • No

    0 0%
  • Maybe

    0 0%
  • No comment

    1 50.00%
Results 1 to 6 of 6

Thread: [RESOLVED] SQL Server 2000 Query Help returning first attempted record

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    Wonder Lake, IL
    Posts
    38

    Resolved [RESOLVED] SQL Server 2000 Query Help returning first attempted record

    SQL Server 2000.

    I am trying to write a query that obtains the first record submitted by a user. The user can take a test as many times as they want I just need to know how to obtain the first attempt.

    Here is my first attempt and it returns multiple attemps though. A nudge in the correct direction would be greatly appreciated



    SELECT Distinct tbl_security.UserId,
    tbl_security.Username,
    tbl_coursecodes.coursecode,
    tbl_quizresults.surveycode,
    tbl_quizresults.score,
    MIN(tbl_quizresults.startdate)




    FROM

    CPTD.dbo.tbl_coursecodes tbl_coursecodes
    join CPTD.dbo.tbl_surveycodes tbl_surveycodes
    on tbl_coursecodes.coursecode = tbl_surveycodes.coursecode
    join CPTD.dbo.tbl_surveyquizzes tbl_surveyquizzes
    on tbl_surveycodes.rowid = tbl_surveyquizzes.surveycode
    join CPTD.dbo.tbl_quizzes tbl_quizzes
    on tbl_surveyquizzes.quizid = tbl_quizzes.rowid
    join CPTD.dbo.tbl_quizversions tbl_quizversions
    on tbl_quizzes.rowid = tbl_quizversions.quizid
    join CPTD.dbo.tbl_quizresults tbl_quizresults
    on tbl_quizversions.rowid = tbl_quizresults.quizid
    join CPTD.dbo.tbl_security tbl_security
    on tbl_security.rowid = tbl_quizresults.userid


    Where tbl_quizresults.startdate >= '2006-09-01'
    AND tbl_coursecodes.coursecode = 'CSS100012'


    Group By tbl_security.UserId,
    tbl_security.Username,
    tbl_coursecodes.coursecode,
    tbl_quizresults.surveycode,
    tbl_quizresults.score
    Last edited by Hack; Jan 11th, 2007 at 12:15 PM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: SQL Server 2000 Query Help returning first attempted record

    The 'first' record in the database may not be the first one saved. Either save the date/time and retrieve the earliest date/time record for that user, or number the records (an autonumber field will do) and retrieve the lowest number record for that user.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    Wonder Lake, IL
    Posts
    38

    Re: SQL Server 2000 Query Help returning first attempted record

    The datetime is saved as startdate. I am not sure how to write the query to return

    tbl_security.UserId,
    tbl_quizresults.score As First_Score,

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL Server 2000 Query Help returning first attempted record

    The way to do it is to use a sub-query to limit the results for tbl_quizresults (as it is where Score comes from) the to only the lowest date for that user.

    Luckily you have the userid and startdate in that table too, so it will be a short sub-query. Simply add this to your Where clause:
    Code:
    AND tbl_quizresults.startdate = (
        SELECT Min(qr2.startdate)
        FROM CPTD.dbo.tbl_quizresults qr2
        WHERE qr2.userid = tbl_quizresults.userid
        )

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2006
    Location
    Wonder Lake, IL
    Posts
    38

    Re: SQL Server 2000 Query Help returning first attempted record

    Thanks That was exactly what I needed!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL Server 2000 Query Help returning first attempted record

    Good stuff, glad to have helped.

    As you have the answer, could you please do us a little favour, and mark this thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do this by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width