一尘不染

SQL将行变成列并填充值

sql

我不太确定为什么用这种方式设计这张桌子,但是这很难解决我的问题。查看数据:

NAME            TYPE_NAME               DEFAULT_VALUE           VALUE
TEST 1          Currency                Null                    14
TEST 1          Event Count             0                       0
TEST 1          Usage                   8                       Null
TEST 1          Events Amt              0                       0
TEST 1          Usage Amt               Null                    13
TEST 1          From Date               Null                    5
TEST 1          To Date                 6                       Null
TEST 1          Traffic Scenario        Null                    2
TEST 1          Band                    1                       Null
TEST 1          Service                 15                      Null
TEST 1          Tariff Rate Name        Null                    4
TEST 2          Currency                EUR                     0
TEST 2          Event Count             Null                    9
TEST 2          Usage                   10                      Null
TEST 2          Events Amt              Null                    13
TEST 2          Usage Amt               Null                    14
TEST 2          From Date               Null                    3
TEST 2          To Date                 4                       Null
TEST 2          Traffic Scenario        Null                    5
TEST 2          Band                    6                       Null
TEST 2          Service                 7                       Null
TEST 2          Tariff Rate Name        Null                    8
TEST 3          Currency                USD                     0
TEST 3          Event Count             0                       0
TEST 3          Usage                   4                       Null
TEST 3          Events Amt              0                       0
TEST 3          Usage Amt               Null                    5
TEST 3          From Date               Null                    1
TEST 3          To Date                 2                       Null
TEST 3          Traffic Scenario        13603                   0
TEST 3          Band                    3                       Null
TEST 3          Service                 3                       Null
TEST 3          Tariff Rate Name        24HR                    0

我需要帮助的是执行以下操作:对于每个名称,将type_name作为一列,然后用default_value或value填充这些列。

像这样:

NAME    Currency        Event Count     Usage   Events Amt      Usage Amt       From Date       To Date Traffic Scenario        Band    Service Tariff Rate Name
TEST 1  14              0               8       0               13              5               6       2                       1       15      4
TEST 2  EUR             9               10      13              14              3               4       5                       6       7       8
TEST 3  USD             0               4       0               5               1               2       13603                   3       3       24HR

基本上从default_value或value填充,取决于哪一个不为null,如果两者均为零,则仅为0。

任何人都可以帮我解决这个问题,因为这里缺少我的SQL知识。

非常感谢!


阅读 169

收藏
2021-05-16

共1个答案

一尘不染

您可以使用PIVOT以下方法:

SELECT * FROM
( SELECT NAME, TYPE_NAME, COALESCE(VALUE, DEFAULT_VALUE) AS VAL
    FROM YOUR_TABLE
) 
PIVOT 
( MAX ( VAL )
  FOR TYPE_NAME IN ( 'Currency' AS CURRENCY, 'Event Count' AS EVENT_COUNT, 
                       'Usage' AS USAGE_, 'Events Amt' AS EVENTS_AMT )
)

干杯!!

2021-05-16