简体   繁体   English

如何基于两个表创建新列和新行?

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

I have two tables:我有两张桌子:

Table 1表格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:表 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:我想要的 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 .这个想法是,表 2 有很多行和产品,但没有Market Table 1 helps you find out which product in Table 2 belongs to which Market-based on the Joining column .表 1 帮助您根据Joining column找出表 2 中的哪个产品属于哪个 Market-。 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.例如,表 1 中存在 3 个市场,然后我想在表 2 中分配一个新列市场,以便表 2 中具有 B1C1 的 ATC4 代码的所有产品都属于市场 A1。 Why?为什么? Because in Table 1, it says that Market A1 should follow the Joining Column of ATC4 - which corresponds to the code B1C1.因为在表 1 中,它说 Market A1 应该遵循 ATC4 的 Joining Column - 对应于代码 B1C1。 In Table 1, we also provided a Product column, this is just for our purpose of identifying our own companies product name.在表 1 中,我们还提供了一个 Product 列,这只是为了标识我们自己公司的产品名称。 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!现在如果你看到表 1 有两行 Market A2,ATC4 不同,这是很正常的,因为可能 Product D2 和 D10 属于 Market A2,但两者可能包含不同的 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:还有一个细微差别,我们有家庭A,这只是A1 + A2的组合,但在我的表2中。没有这样的行值总结家庭A:所以我需要实现两件事:

  1. I want to make a new column Market in Table 2 so that each product is mapped to the market.我想在表 2 中创建一个新列 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.我想创建额外的行来说明市场系列 A (A1+A2) 并将产品名称称为“可爱的家庭 A”或其他名称。 The above table 3 provides an expected output.上表 3 提供了预期的 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.由于我是 SQL 的新手,所以我尝试先使用CASE语句,到 map 慢慢地一个一个,但很快就变得乏味,不知道是否有一些技巧。

My CASE looks like this我的 CASE 看起来像这样

  ,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.但还没有弄清楚如何添加我可以总结 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.我认为没有理由重复values列。 And values is a really bad name for a column because it is a SQL keyword. values对于列来说是一个非常糟糕的名称,因为它是一个 SQL 关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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