Solved

select distinct multiple collumns

Posted on 2004-08-07
7
1,089 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

810 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