Solved

select distinct multiple collumns

Posted on 2004-08-07
7
1,088 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now