1

Consider the below sample table:

ID    Value
123   ABC
456   DEF
456   ABC

I want the select query result as below:

ID    Value
123   ABC
456   DEF

Note: ID has only 2 different values - "123" and "456". Selection should be based on Column "ID". If value "123" is present, the corresponding data from "Value" column has to be selected. If not "456" ID should be retrieved.

2
  • post the actual ID value if it is a string Commented Mar 20, 2019 at 6:47
  • What do you mean by " If value "123" is present ? with respect to what ? You said Id only has 123 or 456, so it should be present isn't it? Commented Mar 20, 2019 at 6:57

3 Answers 3

1

simple group by will help you to get the desired result

select min(id), value
from table
group by value
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry forget to mention that ID is not a number. It is a string. So min(id) is not helpful.
@Viswa cast it as a numeric use TO_NUMBER
min function can be applied not only for numbers.
min function will not work if 123 is yyy and 456 is xxx
@psaraj12 . . . Then use max(). It is only possible to answer the question that is asked, and this seems like this simplest answer.
|
0

Some thing like this:

    select min(id) as id,
           Value 
    from table
    group by  Value, id

Comments

0

You can use the below if ID is a string like 456 is 'xxx' and 123 is 'yyy' The SQL fiddle here

WITH tt1 
     AS (SELECT Decode(id, '123', 1, 
                           2) order1, 
                id, 
                value1 
         FROM   tt), 
     tt2 
     AS (SELECT Min(order1) order1, 
                value1 
         FROM   tt1 
         GROUP  BY value1) 
SELECT tt1.id, 
       tt1.value1 
FROM   tt2, 
       tt1 
WHERE  tt1.value1 = tt2.value1 
       AND tt1.order1 = tt2.order1; 

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.