2

I have a simple query output of two rows and single column

Virginia
Texas

I want the output as

Virginia   |  Texas

I just tried it as two subqueries in column list.

select 
(select state from table where code='VA') as state1
(select state from table where code='TX') as state2
from tablename

Is there better way to get the result

1
  • That's not a bad solution. Commented Jul 26, 2019 at 5:05

1 Answer 1

1

Are you looking for string aggregation?

select string_agg(state, ' | ' order by code desc)
from table
where code in ('VA', 'TX')

You can use conditional aggregation:

select max(state) filter (where code = 'VA'),
       max(state) filter (where code = 'TX')       
from t
where code in ('VA', 'TX')
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I'm actually looking for two columns as output. column1 should have VA and column2 should have TX

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.