Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select distinct multiple collumns

Posted on 2004-08-07
7
Medium Priority
?
1,095 Views
Last Modified: 2012-05-05
Okay guys this is really freakin problems. Suppose I have a table like this

-------------------------------------------
prod_name | price | isbn | details
-------------------------------------------
AA             | 22.0  |655434 | pdetail
AA             | 22.0  |655434 | pdetail
AA             | 22.0  |655434 | Jdetail
BB             | 24.0  |655436 | mdetail
BB             | 24.0  |655436 | mdetail
CC             | 25.0  |655430 | udetail
CC             | 25.0  |655430 | udetail

How do I select a row from the table that has same rows containing prod_name, price, details.

I mean I want the output from the table above as

-------------------------------------------
prod_name | price | isbn | details
-------------------------------------------
AA             | 22.0  |655434 | pdetail
AA             | 22.0  |655434 | Jdetail
BB             | 24.0  |655436 | mdetaill
CC             | 25.0  |655430 | udetail

Will u guys plz help me out.
0
Comment
Question by:dark_in_lights
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 8

Expert Comment

by:sapnam
ID: 11742437
SELECT DISTINCT a.prod_name,a.price,a.isbn,a.details
from your table a
where exists (select b.prod_name,b.price,b.isbn,b.details
                       from your table b
                     where b.prod_name = a.prod_name
                         and b.price = a.price
                         and b.isbn = a.isbn
                         and b.details = a.details
                         and b.rowid <> a.rowid);
0
 
LVL 8

Expert Comment

by:sapnam
ID: 11742458
Or rather why can't it simply be

SELECT DISTINCT a.prod_name,a.price,a.isbn,a.details
from your table a
0
 
LVL 8

Accepted Solution

by:
sapnam earned 1000 total points
ID: 11742467
The first query I posted will give you those rows which are having duplicates in the data.

I tried it out with your test data and the result is

AA        22    655434 pdetail
BB        24    655436 mdetail
CC        25    655430 udetail

So it does not give you

AA        22    655434 jdetail

If you want a listing of all distinct values then you give the second query
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 1000 total points
ID: 11742708
but if you want "duplicates"

then just do

SELECT a.prod_name,a.price,a.isbn,a.details
from your table a
group by a.prod_name,a.price,a.isbn,a.details
having count(*) > 1
0
 
LVL 7

Expert Comment

by:cy_hung
ID: 11745104
A simpler version :

SELECT prod_name price, isbn, details
from table group by prod_name, price, isbn, details

This query should work in IBM DB2 and most popular database but not all .... The behaviour may be differentin MySQL or Sybase ...

By the way, how do you use the result, for report generation or within an application ?

This feature is common is most BI tools, such as Cognos, MicroStrategy and BusinessObjects...  these tools save a lot of times for generate reports.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question