简体   繁体   中英

How to create new column and new row based on two tables?

I have two tables:

Table 1

    MARKET      ATC3     ATC4    PRODUCT    BOOLEAN FLAG    JOINING COLUMN
    A1          B1     B1C1       D1           1                     ATC4
    A2          B1     B1C2       D2           1                     ATC4
    A2          B1     B1C3                                          ATC4
    FAMILY A    B1                                                   ATC3

Table 2:

PRODUCT ATC3    ATC4    VALUES
D1       B1   B1C1  10
D1       B1   B1C1  20
D2       B1   B1C2  15
D2       B1   B1C2  25
D2       B1   B1C2  10
D3       B1   B1C3  5

My desired output:

PRODUCT       ATC3  ATC4    VALUES  MARKET  VALUES
D1             B1     B1C1  10       A1     10
D1             B1     B1C1  20       A1     20
D2             B1     B1C2  15       A2     15
D2             B1     B1C2  25       A2     25
D2             B1     B1C2  10       A2     10
D3             B1     B1C3  5        A2      5
ALL D1+D2+D3                         FAMILY A   85

The idea is, Table 2 has many rows and products but does not have Market . Table 1 helps you find out which product in Table 2 belongs to which Market-based on the Joining column . For example, There are 3 Markets present in Table 1, I want to then assign a new column Market in Table 2 such that all PRODUCTS in Table 2 with the ATC4 code of B1C1 belongs to the Market A1. Why? Because in Table 1, it says that Market A1 should follow the Joining Column of ATC4 - which corresponds to the code B1C1. In Table 1, we also provided a Product column, this is just for our purpose of identifying our own companies product name. Now if you see that for Table 1, there are two rows of Market A2, with different ATC4, this is very normal, because maybe Product D2 and D10 belong to Market A2, but both may contain different ATC4!

There is also one more nuance to it, we have Family A, This is merely a combination of A1+A2, but in my Table 2. there is no such row value that sums up to Family A: So I need to achieve two things:

  1. I want to make a new column Market in Table 2 so that each product is mapped to the market.

  2. I want to create extra rows to account for the Market Family A (A1+A2) and call the product Name "Lovely Family A" or something. The above table 3 provides an expected output.

Since I am new to SQL, I tried to first use CASE Statements, to map slowly one by one, but soon it gets tedious and I wonder if there's some tricks.

My CASE looks like this

  ,CASE WHEN ATC4 LIKE '%B1C1%' THEN 'A1' 
         WHEN ATC4 LIKE '%B1C2%' OR ATC4 LIKE '%B1C3%' THEN 'A2'  ELSE ATC4 END AS MARKET_NAME

but have yet to figure out how to add the additional row where I can sum up A1+A2.

You seem to want something like this:

with rows as (
      select PRODUCT, ATC3, ATC4, VALUES
      from table2
      union all
      select 'ALL D1+D2+D3', ATC3, NULL, SUM(VALUES)
      from table2
      group by ATC3
     )
select r.*, t1.market
from rows r join
     table1 t1
     on (t1.joining_column = 'ATC3' and t1.atc3 = r.atc3) or
        (t1.joining_column = 'ATC4' and t1.atc4 = r.atc4);

I see no reason to repeat the values column. And values is a really bad name for a column because it is a SQL keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM