2

Given with the query below

Select COLUMN_ID,  (Select 
CASE COLUMN_ID
WHEN 4 THEN 'WEIGHT'
WHEN 6 THEN 'CARGO_LENGTH'
WHEN 7 THEN 'WIDTH'
WHEN 8 THEN 'HEIGHT'
END 
GROOVE
FROM ALL_TAB_COLS where TABLE_NAME = 'TBL_CARGO')
FROM ALL_TAB_COLS where COLUMN_ID IN(4,6,7,8)

I like to get only non blank columns. I'm expecting the output 4 6 7 8 displayed per field. How do I do that?

4
  • 1
    Get rid off the in-line view. Do it in single query, no sub-query is required. Post your input and desired output. Commented Jul 3, 2015 at 11:12
  • Your subselect returns one row per column in the table TBL_CARGO. It's unclear to me what you're trying to accomplish - could you please add sample input (or the DDL statement for TBL_CARGO) and expected output? Commented Jul 3, 2015 at 11:13
  • 2
    Why don't you just select the column_name directly ? Commented Jul 3, 2015 at 11:16
  • @FrankSchmitt Lalit Kumar's solved my problem thanks for comments. Commented Jul 3, 2015 at 11:20

3 Answers 3

2

All that sub-query and in-line view could be done in single query:

Using CASE expression(verbose and easy to understand):

SELECT COLUMN_ID,
  CASE COLUMN_ID
    WHEN 4
    THEN 'WEIGHT'
    WHEN 6
    THEN 'CARGO_LENGTH'
    WHEN 7
    THEN 'WIDTH'
    WHEN 8
    THEN 'HEIGHT'
  END GROOVE
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'TBL_CARGO'
AND COLUMN_ID   IN(4,6,7,8);

Using DECODE(looks short):

SELECT COLUMN_ID,
  DECODE(COLUMN_ID, 4, 'WEIGHT', 6, 'CARGO_LENGTH', 7, 'WIDTH', 8, 'HEIGHT')
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'TBL_CARGO'
AND COLUMN_ID   IN(4,6,7,8);
Sign up to request clarification or add additional context in comments.

1 Comment

@TDL Glad it helped, please mark it as answered, would help others.
0

No need for a sub-select, just add the CASE expression. Something like this perhaps?

Select COLUMN_ID,  
       CASE COLUMN_ID WHEN 4 THEN 'WEIGHT'
                      WHEN 6 THEN 'CARGO_LENGTH'
                      WHEN 7 THEN 'WIDTH'
                      WHEN 8 THEN 'HEIGHT'
       END  GROOVE
FROM ALL_TAB_COLS
where COLUMN_ID IN(4,6,7,8)

2 Comments

Why did you remove the filter on table_name?
Good question, I couldn't find out where to move it from the sub-select... Easy to add if required.
0

Try this

Select COLUMN_ID, 
CASE COLUMN_ID
WHEN 4 THEN 'WEIGHT'
WHEN 6 THEN 'CARGO_LENGTH'
WHEN 7 THEN 'WIDTH'
WHEN 8 THEN 'HEIGHT'
END 
GROOVE
FROM ALL_TAB_COLS where TABLE_NAME = 'TBL_CARGO'
and COLUMN_ID IN(4,6,7,8)

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.