Results 1 to 11 of 11

Thread: [RESOLVED]Distinct Help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Question [RESOLVED]Distinct Help

    Hi all,

    I need to know if this is possible. I want to run a select statement that selects only the first 4 characters of the field and make sure there are no duplicates.

    Examples of some of the data:

    1234-01
    1234-02
    1234-03
    2222-01
    2222-03

    You see all I want returned is 1234 & 2222.

    Any ideas? I have tried "Select Distinct left(Ref,4) from Table" but this does not work

    Thanks
    Last edited by drawlings; Aug 13th, 2008 at 10:19 AM.

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Distinct Help

    select DISTINCT left(Ref,4) from Table should work properly,can you tell us what ouput you get when you run this query?
    __________________
    Rate the posts that helped you

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Distinct Help

    How about
    Code:
    Select Left(Ref,4) from Table
    GROUP BY ref

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Smile [RESOLVED] Distinct Help

    Perfect they both worked. I did try that before posting the original comment and it did not work. Oh well, thanks for your help guys.

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Distinct Help

    Quote Originally Posted by Hack
    How about
    Code:
    Select Left(Ref,4) from Table
    GROUP BY ref
    this wont worked,considering OP's sample data it will return

    Code:
    1234
    1234
    1234
    2222
    2222
    __________________
    Rate the posts that helped you

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED]Distinct Help

    The GROUP BY will weed out the duplicates.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: [RESOLVED]Distinct Help

    Both examples worked for me. So i'm happy

  8. #8
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [RESOLVED]Distinct Help

    Quote Originally Posted by Hack
    The GROUP BY will weed out the duplicates.
    hack still i am confused how Group by query will give 1234 & 2222

    should't it should be


    Code:
    Select Left(Ref,4) from Table
    GROUP BY Left(Ref,4) 
    or

    Code:
    Select DISTINCT Left(Ref,4) from Table
    GROUP BY ref
    __________________
    Rate the posts that helped you

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: [RESOLVED]Distinct Help

    I have just looked at Hacks orignal post, I added the DISTINCT before the left command.

    Code:
    Select DISTINCT Left(Ref,4) from Table GROUP BY Ref

  10. #10
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [RESOLVED]Distinct Help

    Quote Originally Posted by drawlings
    I have just looked at Hacks orignal post, I added the DISTINCT before the left command.

    Code:
    Select DISTINCT Left(Ref,4) from Table GROUP BY Ref
    you already have DISTINCT clause so dont use Group by as it will be sort of overkilling your query
    __________________
    Rate the posts that helped you

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: [RESOLVED]Distinct Help

    Thanks mate, will remove the Group By Clause

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