|
-
Aug 13th, 2008, 10:04 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Aug 13th, 2008, 10:08 AM
#2
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 
-
Aug 13th, 2008, 10:11 AM
#3
Re: Distinct Help
How about
Code:
Select Left(Ref,4) from Table
GROUP BY ref
-
Aug 13th, 2008, 10:23 AM
#4
Re: Distinct Help
 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 
-
Aug 13th, 2008, 10:19 AM
#5
Thread Starter
Hyperactive Member
[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.
-
Aug 13th, 2008, 10:24 AM
#6
Re: [RESOLVED]Distinct Help
The GROUP BY will weed out the duplicates.
-
Aug 13th, 2008, 10:33 AM
#7
Re: [RESOLVED]Distinct Help
 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 
-
Aug 13th, 2008, 10:38 AM
#8
Thread Starter
Hyperactive Member
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
-
Aug 13th, 2008, 10:59 AM
#9
Re: [RESOLVED]Distinct Help
 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 
-
Aug 13th, 2008, 10:28 AM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED]Distinct Help
Both examples worked for me. So i'm happy
-
Aug 13th, 2008, 11:05 AM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|