1

I want to select row IDs associated with distinct column combinations in the remainder of a table. For instance, if the distinct rows are

enter image description here

I want to get the row IDs associated with each row. I can't query for distinct IDs since they are the row's primary key (and hence are all distinct).

So far I have:

SELECT e.ID 
FROM E_UPLOAD_TEST e
INNER JOIN (
  SELECT DISTINCT WHAT, MATERIALS, ERROR_FIELD, UNITS, SEASONALITY, DATA_TYPE, DETAILS, METHODS, DATA_FORMAT 
  FROM E_UPLOAD_TEST) c
ON e.WHAT = c.WHAT AND e.MATERIALS = c.MATERIALS AND e.ERROR_FIELD = c.ERROR_FIELD AND e.DATA_TYPE = c.DATA_TYPE AND e.METHODS = c.METHODS AND e.DATA_FORMAT = c.DATA_FORMAT;  

which runs but doesn't return anything. Am I missing a GROUP BY and/or MIN() statement?

5
  • 1
    If you consider NULLs as matching values in combinations then you need handle it properly. NULL=NULL is false. See asktom.oracle.com/pls/asktom/… Commented Jun 30, 2017 at 18:17
  • Can you give an example of what you are wanting the output to be? which rows in your data set would you not want row IDs for? Commented Jun 30, 2017 at 19:08
  • Please read meta.stackoverflow.com/questions/285551/… and the accepted answer Commented Jun 30, 2017 at 20:08
  • I assume this has nothing to do with PLSQL, so I will edit the title. Commented Jun 30, 2017 at 20:36
  • "I want to get the row ID's" probably means there are row id's somewhere in the table? Or do you mean the ROWID assigned by Oracle when the table is stored on disk? And, HOW do you want to get the ID's? In what form? What should the output look like? Commented Jun 30, 2017 at 20:39

2 Answers 2

1

@serg is correct. Every single row in your example has at least one column value that is null. That means that no row will match your join condition. That is why your query results in no rows found.

Modifying your condition might get you what you want so long has your data isn't changing frequently. If it is changing frequently, then you probably want a single query for the entire job otherwise you'll have to set your transaction so that it is immune to data changes.

An example of such a condition change is this: ( (e.WHAT is null and c.WHAT is null) or (e.WHAT = c.WHAT) )

But such a change makes sense only if two rows having a null value in the same column means the same thing for both rows and it has to mean the same thing as time marches on. What "WHAT is null" means today might not be the same thing tomorrow. And that is probably why C. J. Date hates nulls so much.

Sign up to request clarification or add additional context in comments.

1 Comment

I updated the NULL cells to the string 'n/a', since they have the same "meaning" for the data in table. Thanks to you and @Serg for the reminder on NULLs.
0

Instead of comparing, use the decode function which compares two null values correctly.

e.WHAT = c.WHAT -> DECODE(e.WHAT, c.WHAT, 1) = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.