Results 1 to 10 of 10

Thread: Is there a "First" command in SQL?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Is there a "First" command in SQL?

    In access, I can add the First command to a select query in order to pull the first occurance of a unique record, but there does not seem to be a First command in SQL Server. Does anyone know the equivalent?

    So for example, say I have the following data:

    0001 | Dan Bassett | This is a test
    0001 | Dan Bassett | This is another test

    If I were to do a Select on this, both records would return, but how would I limit it to just the first record?

    Visual Studio 2010

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    A SELECT DISTINCT should do the trick for you...

  3. #3
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by Hack
    A SELECT DISTINCT should do the trick for you...
    I believe the Distinct keyword is row-based, and it would view those two rows as distinct entries.

    Try a "Select Top 1 * From Tablename... Where..." and so on.
    Last edited by Sheppe; Sep 24th, 2002 at 06:55 PM.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    You are correct Sheppe, the DISTINCT keyword is row based.

    My assumption was that that was the basis for the question. Your code works very well if I was incorrect.

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Anyway... SELECT DISTINCT UserID, UserName From Table would also do the trick.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by Mc Brain
    Anyway... SELECT DISTINCT UserID, UserName From Table would also do the trick.
    Not if the rows themselves are distinct, as in his example.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    The Id and name are the same in his example. Obviously... if he doesn't add a WHERE clause, it will retrieve all the users (but, only once each)
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by Mc Brain
    The Id and name are the same in his example. Obviously... if he doesn't add a WHERE clause, it will retrieve all the users (but, only once each)
    Yeah, but judging by his example, I'd say he wants all three columns returned, not just the two that you are selecting.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Ok, in that case... only the TOP 1 would work for him.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by Mc Brain
    Ok, in that case... only the TOP 1 would work for him.
    Technically he could also do the following:

    Set rowcount 1
    Select ....
    Set rowcount 0

    But the Top 1 makes more sense.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

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