LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2011, 05:57 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
MS Access SELECT Statement to Exclude Duplicate Values in a Specified Column


Hi,


Please consider this data/table consisting of 3 Columns with the 2nd Column having Duplicate Values (Book Titles here) :

Note:SELECT * FROM Books

Code:
Book_ID	Book_Title	Tags
1	C	Pointers
2	VB	Programming
3	C	DS
4	Java	Multi Threading
5	PHP	Website
6	SQL	SQL
7	SQL	SQL
8	C++	OOPs
9	Oracle	RDBMS
10	VB	Software Development

Let's Sort the Data/Table on the 2nd Column i.e. book_title:

Code:
Book_ID	Book_Title	Tags
1	C	Pointers
3	C	DS
8	C++	OOPs
4	Java	Multi Threading
9	Oracle	RDBMS
5	PHP	Website
6	SQL	SQL
7	SQL	SQL
2	VB	Programming
10	VB	Software Development
We see that Book_ID: 3, 7, and 10 above have duplicated values in their respective Book_Title fields.

In the result/output we do want all the 3 Columns (or all the columns of the table) with the condition that if any Book_Title is coming again down the list then only the first one needs to be SELECTed. In other words, we want to exclude duplicate book titles irrespective of what values the other corresponding columns may have.

So, the desired output should look like as follows:


Code:
Book_ID	Book_Title	Tags
1	C	Pointers
8	C++	OOPs
4	Java	Multi Threading
9	Oracle	RDBMS
5	PHP	Website
6	SQL	SQL
2	VB	Programming
Please Note: In the first table above, the Book IDs 6 and 7 show identical records. But that is not the case with the other records. So, whether 2 or more records are identical or not (DISTINCTROW, in MS Access), we are concerned about a specific column's values and want them to be UNIQUE in our Result/Output. Kind of GROUP BY Query but that would not help us display all the fields/columns.

Do we need some Sub-Query or Join here? Any ideas, gurus?

Last edited by devUnix; 12-13-2011 at 06:00 AM.
 
Old 12-14-2011, 02:07 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
this looks like homework to me, so I won't say too much, but you do want a GROUP BY to do this.
 
Old 12-14-2011, 08:29 AM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by acid_kewpie View Post
this looks like homework to me, so I won't say too much, but you do want a GROUP BY to do this.
Yes, I mentioned it already that it does require a GROUP BY query but that alone would not fetch all the columns. Some logic is missing here.
 
Old 12-14-2011, 10:19 AM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
'greatest-n-per-group'
and/or 'having'
 
Old 12-14-2011, 10:45 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Yes, so you just ask for the remaining columns as well, selecting appropriately from each grouped set to find an appropriate value.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] if statement perl for multiple values priyophan Linux - Software 8 06-08-2011 03:58 PM
[SOLVED] Delete rows based on values in a column using sed captainentropy Linux - Newbie 6 01-19-2011 08:59 AM
remove duplicate entries from first column?? kadvar Programming 2 05-12-2010 06:22 PM
Bash select statement and spaces meshcurrent Linux - General 2 04-09-2003 09:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration