Skip to content
Advertisement

sql Tables manipulation

tables

Hi everyone,

i have the above table ‘given table’ i would like to create a new table with columns ‘product’, ‘paid’ , ‘branchA’ and ‘branchB’ as shown in the ‘desired output table’ with the unique identifier column ‘id’. This must be a simple sql task, but here i am. Any constructive suggestions or code samples are welcome. thx in advance

Advertisement

Answer

There are probably a number of ways to do this. Make sure that you fully understand whichever one you choose.

My approach is to union the first set of columns together with the second set.

SELECT *
INTO   new_table
FROM   (
           SELECT
                id
              , T_product AS [product]
              , T_paid    AS [paid]
              , T_branchA AS [branchA]
              , T_branchB AS [branchB]
           FROM given_table
           UNION
           SELECT
                id
              , S_product AS [product]
              , S_paid    AS [paid]
              , S_branch  AS [branchA]
              , S_branch2 AS [branchB]
           FROM given_table
       ) x;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement