SQL 将包含 table 名称的数据 table 中的数据插入到多个 table 中,列名和值列映射可以在源中更改

SQL Insert data into multiple tables from data table that contains table name, column name and value column mapping can change in source

我很困惑。这看起来可行,但不确定如何实现。
我有一个看起来像这样的数据 table。数据源有时会 交换源文件中的列。

Data                            
    IN      column          table_sheet          y      w       value   
    col1    Route           Summary              2021   Week 1  a   
    col2    CNG/Diesel      Summary              2021   Week 1  5           
    col3    Freq            Summary              2021   Week 1  B       
    col4    Weekly Miles    Summary              2021   Week 1  6       
    col1    CNG/Diesel      Summary              2021   Week 2  1       
    col2    Freq            Summary              2021   Week 3  1       
    col3    Weekly Miles    Summary              2021   Week 4  1       
    col1    Load            Days_ON              2021   Week 1  L210224-25048   
    col2    Load_id         Days_ON              2021   Week 1  L210224-25048
    col3    cost            Days_ON              2021   Week 1  263.64  
    col1    Distance        CCD                  2021   Week 2  781.62      
    col2    Code            CCD                  2021   Week 2  CL      
    col3    Name            CCD                  2021   Week 2  Squre       
    col4    Cost            CCD                  2021   Week 2  1800 

源文件是一个包含多个工作表(Table 项目名称)的电子表格,每周填充一次并且是增量的。 The source file column mapping can change as in table Summary, all columns(excel source file column index) from the source changed .目标 tables 已经有所有可能的列名。 SQL 代码必须读取 table 名称并将数据相应地映射到源 table。

I want to populate the below tables using the data table above like this.               
Summary     Route    CNG/Diesel Freq    Weekly Miles    y       W
            a        5          B       6               2021    Week 1
                     1                                  2021    Week 2
                                1                       2021    Week 3
                                        1               2021    Week 4
                            
Days_ON                 
        Load            LoaD_id         cost    y          W
        L210224-25048   L210224-25048   263.64  2021       Week 1   
                            
                            
CCD                         
        Distance        Code    Name        Cost    y       W
        781.62          CL      Squre       1800    2021    Week 1  

已添加- 该解决方案可能适用于一行,但不适用于同一周的多行。

Data                
IN      column           table_sheet     y      w     value h1_ind
    col1    Route        Summary        2021    Week 1  a   2
    col2    CNG/Diesel   Summary        2021    Week 1  5   3
    col3    Freq         Summary        2021    Week 1  B   4
    col4    Weekly Miles Summary        2021    Week 1  6   5
    col1    Route        Summary        2021    Week 1  b   2
    col2    CNG/Diesel   Summary        2021    Week 1  1   3
    col3    Freq         Summary        2021    Week 1  1   4
    col4    Weekly Miles Summary        2021    Week 1  1   5
    col1    Route        Summary        2021    Week 1  c   2
    col2    CNG/Diesel   Summary        2021    Week 1  5   3
    col3    Freq         Summary        2021    Week 1  B   4
    col4    Weekly Miles Summary        2021    Week 1  6   5
    col1    Route        Summary        2021    Week 1  d   2
    col2    CNG/Diesel   Summary        2021    Week 1  1   3
    col3    Freq         Summary        2021    Week 1  1   4
    col4    Weekly Miles Summary        2021    Week 1  1   5

Desired output:
table_sheet    y      w      Route CNG/Diesel  Freq  Weekly Miles
Summary       2021   Week 1   a       5         B     6
Summary       2021   Week 1   b       1         1     1
Summary       2021   Week 1   c       5         B     6
Summary       2021   Week 1   d       1         1     1

如果可能,SQL unpivot 如何提供此输出?我想看看这个。 我还想看看我们是否可以让勺子工作。 请协助。

您需要用 table_sheet 分隔您的行,然后为每个 table_sheet 应用行反规范化器,因为您生成不同的表: