1

I have a table "TABLE1" like below

Date        src_counrty  src_State   dst_country    dst_state   Value
2022-02-09       a           d           x             y          1
2022-02-09       a           e           x             y          1
2022-02-09       c           f           x             y          1

I want to have output table as below using a select statement

Date         src    dst   Value
2022-02-09    a      x      1
2022-02-09    a      x      1
2022-02-09    c      x      1
2022-02-09    d      y      1
2022-02-09    e      y      1
2022-02-09    f      y      1
2022-02-09    a      y      1
2022-02-09    a      y      1
2022-02-09    c      y      1
2022-02-09    d      x      1
2022-02-09    e      x      1
2022-02-09    f      x      1

As seen in output table column src_counrty and src_State is merged as single column "src" and dst_country and dst_state as "dst" Is there any way to achieve this output using SQL query!

I have searched related topics on the internet and could not find any, so if anyone has any suggestion/solution for this it will be much helpful for me.. Thanks!!

1 Answer 1

2

You may use a series of unions:

SELECT Date, src_counrty, dst_country, Value FROM TABLE1
UNION ALL
SELECT Date, src_State, dst_country, Value FROM TABLE1
UNION ALL
SELECT Date, src_counrty, dst_state, Value FROM TABLE1
UNION ALL
SELECT Date, src_State, dst_state, Value FROM TABLE1
ORDER BY Date;

If the order of the output matters beyond the date, state the logic and the above query can be updated.

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

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.