Solved

select distinct multiple collumns

Posted on 2004-08-07
7
1,092 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 250 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 250 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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