Solved

select distinct multiple collumns

Posted on 2004-08-07
7
1,090 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
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

856 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