SELECT DISTINCT only on some fields but returning other fields.
795321Sep 1 2010 — edited Sep 1 2010As the thread says I need to select distinct rows with some fields but I need to show the associated data of other columns in the same table.
Simple Example
SUPPOSE THE TABLE INFO:
INFO(ID, NAME, TITLE, NUMBER, TELEPHONE, FAX, DESCRIPTION, TOPIC);
The table info could have null values in all the columns except for the ID. And there could be the same information for: Name, Title, Number, Telephone, Fax info in a lot of rows.
With the following statement I get distinct rows based on the name, title, number, telephone, fax and i get assured that the row with name in null is not returned.
SELECT DISTINCT P.NAME, P.TITLE,P.NUMBER,P.TELEPHONE,P.FAX FROM INFO P WHERE TRIM(P.NAME) IS NOT NULL;
returns 1400 rows;
The problem is that I need the DESCRIPTION and TOPIC columns of the 1400 distinct rows.
I have tried the following, but without sucess because the statement is returning 1350 rows, so there are 50 rows losted:
SELECT P.NAME, P.TITLE,P.NUMBER,P.TELEPHONE,P.FAX, P.DESCRIPTION,P.TOPIC
FROM INFO P , (SELECT DISTINCT P.NAME, P.TITLE,P.NUMBER,P.TELEPHONE,P.FAX FROM INFO P WHERE TRIM(P.NAME) IS NOT NULL) R
WHERE P.NAME= R.NAME AND P.TITLE = R.TITLE AND P.NUMBER = R.NUMBER AND P.TELEPHONE = R.TELEPHONE AND P.FAX = R.FAX;
Could anybody give me some help?
pd: Don´t ask why the table has been designed in that way... I'm trying to fix that ¬¬
Edited by: user6483610 on Sep 1, 2010 6:29 PM