SQL Server: создать динамическую таблицу на основе ввода параметров - PullRequest
0 голосов
/ 12 мая 2019

Я хочу создать динамическую таблицу на основе входного параметра от моих пользователей отчетов.При выборе ProfitCenterID я хочу, чтобы это значение ProfitCenterID отображалось как уровень 1, а все дочерние элементы ProfitCenterID - от уровня 2 до уровня xx.

Когда параметр / ProfitCenterID установлен на «Бизнес-единица A »Мне нужна таблица со всеми ProfitcenterID в этой иерархии.У меня есть пример ниже, показывающий желаемый результат.

Мои ProfitCenterID структурированы, как показано в примере данных ниже.Мои фактические производственные данные имеют до 19 уровней, и пользователь может выбрать параметр входного значения из столбца ProfitCenterID.

Мой инструмент создания отчетов - Tableau, и я подключаюсь к своей базе данных MS SQL-сервера с помощью пользовательского запроса SQL с параметром.

Когда:

ProfitcenterID_Level равно 4 (как для «Бизнес-единицы A»), в предложении WHERE должен использоваться столбец ProfitCenterID_Level_04

ProfitcenterID_Level - это 5 (как для «Учетной записи 1») в предложении WHERE должен использоваться столбец ProfitCenterID_Level_05

ProfitcenterID_Level, равный 6 (как и для "D-123400") в предложении WHERE должен использоваться столбец ProfitCenterID_Level_06

Я не могу понять, как создать этот поиск, который дает мне ProfitcenterID_Level на основе значения в ProfitcenterID.

Пример данных и желаемых результатов приведены ниже.

Пример данных:

CREATE TABLE [Sample]
(
    ProfitCenterID nchar(30)
    ,ProfitCenterID_Level smallint
    ,ProfitCenterID_Level_01 nchar(30)
    ,ProfitCenterID_Level_02 nchar(30)
    ,ProfitCenterID_Level_03 nchar(30)
    ,ProfitCenterID_Level_04 nchar(30)
    ,ProfitCenterID_Level_05 nchar(30)
    ,ProfitCenterID_Level_06 nchar(30)
    ,ProfitCenterID_Level_07 nchar(30)
)

INSERT INTO [Sample]
VALUES ('Business Unit A','4','Global Enterprise','Europe','Denmark','Business Unit A','','',''),
('Account 1','5','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','',''),
('D-123400','6','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123400',''),
('D-123410','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460','D-123410'),
('D-123420','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460','D-123420'),
('D-123430','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460','D-123430'),
('D-123440','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460','D-123440'),
('D-123450','6','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123450',''),
('D-123460','6','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460',''),
('D-123465','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123465'),
('D-123470','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460','D-123470'),
('D-123480','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123460','D-123480'),
('D-123483','6','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123483',''),
('D-123485','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123485'),
('D-123486','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123486'),
('D-123487','6','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123487',''),
('D-123488','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123488'),
('D-123489','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123489'),
('D-123490','6','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490',''),
('D-123491','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123491'),
('D-123492','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123492'),
('D-123493','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123493'),
('D-123494','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123494'),
('D-123495','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123495'),
('D-123496','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123496'),
('D-123497','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123497'),
('D-123498','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123498'),
('D-123499','7','Global Enterprise','Europe','Denmark','Business Unit A','Account 1  ','D-123490','D-123499')

enter image description here

Желаемый результат (при выборе «Бизнес-единица A» - Уровень 4 ):

    SELECT [ProfitCenterID]
          ,[ProfitCenterID_Level]
          -- ,[ProfitCenterID_Level_01]
          -- ,[ProfitCenterID_Level_02]
          -- ,[ProfitCenterID_Level_03]
          ,[ProfitCenterID_Level_04] AS 'Level 01'
          ,[ProfitCenterID_Level_05] AS 'Level 02'
          ,[ProfitCenterID_Level_06] AS 'Level 03'
          ,[ProfitCenterID_Level_07] AS 'Level 04'
      FROM [MyDatabase].[dbo].[Sample]
      WHERE ProfitCenterID_Level_04 = 'Business Unit A' 
 /****** 'Business Unit A' is the input parameter from my report user  ******/

enter image description here

Желаемый результат (при выборе «Accou»nt 1 "- Уровень 5 ):

    SELECT [ProfitCenterID]
          ,[ProfitCenterID_Level]
          -- ,[ProfitCenterID_Level_01]
          -- ,[ProfitCenterID_Level_02]
          -- ,[ProfitCenterID_Level_03]
          -- ,[ProfitCenterID_Level_04]
          ,[ProfitCenterID_Level_05] AS 'Level 01'
          ,[ProfitCenterID_Level_06] AS 'Level 02'
          ,[ProfitCenterID_Level_07] AS 'Level 03'
      FROM [MyDatabase].[dbo].[Sample]
      WHERE ProfitCenterID_Level_05 = 'Account 1' 
/****** 'Account 1' is the input parameter from my report user  ******/

enter image description here

Ответы [ 2 ]

0 голосов
/ 15 мая 2019

Я смог воссоздать отчет с образцом и достиг требуемой функциональности.

При необходимости я могу поделиться рабочей книгой

0 голосов
/ 14 мая 2019

Мне наконец удалось решить мою проблему. Спасибо всем за ваш вклад. Вот как я это решил.

SELECT S.ProfitCenterID, S.ProfitCenterID_Level
    ,CASE 
        WHEN L.ProfitCenterID_Level = 1 THEN S.ProfitCenterID_Level_01
        WHEN L.ProfitCenterID_Level = 2 THEN S.ProfitCenterID_Level_02
        WHEN L.ProfitCenterID_Level = 3 THEN S.ProfitCenterID_Level_03
        WHEN L.ProfitCenterID_Level = 4 THEN S.ProfitCenterID_Level_04
        WHEN L.ProfitCenterID_Level = 5 THEN S.ProfitCenterID_Level_05
        WHEN L.ProfitCenterID_Level = 6 THEN S.ProfitCenterID_Level_06
        WHEN L.ProfitCenterID_Level = 7 THEN S.ProfitCenterID_Level_07
    END AS [Level 1]
    ,CASE 
        WHEN L.ProfitCenterID_Level = 1 THEN S.ProfitCenterID_Level_02
        WHEN L.ProfitCenterID_Level = 2 THEN S.ProfitCenterID_Level_03
        WHEN L.ProfitCenterID_Level = 3 THEN S.ProfitCenterID_Level_04
        WHEN L.ProfitCenterID_Level = 4 THEN S.ProfitCenterID_Level_05
        WHEN L.ProfitCenterID_Level = 5 THEN S.ProfitCenterID_Level_06
        WHEN L.ProfitCenterID_Level = 6 THEN S.ProfitCenterID_Level_07
        -- WHEN L.ProfitCenterID_Level = 7 THEN S.ProfitCenterID_Level_08
    END AS [Level 2]
    ,CASE 
        WHEN L.ProfitCenterID_Level = 1 THEN S.ProfitCenterID_Level_03
        WHEN L.ProfitCenterID_Level = 2 THEN S.ProfitCenterID_Level_04
        WHEN L.ProfitCenterID_Level = 3 THEN S.ProfitCenterID_Level_05
        WHEN L.ProfitCenterID_Level = 4 THEN S.ProfitCenterID_Level_06
        WHEN L.ProfitCenterID_Level = 5 THEN S.ProfitCenterID_Level_07
        --WHEN L.ProfitCenterID_Level = 6 THEN S.ProfitCenterID_Level_08
        --WHEN L.ProfitCenterID_Level = 7 THEN S.ProfitCenterID_Level_09
    END AS [Level 3]
    ,CASE 
        WHEN L.ProfitCenterID_Level = 1 THEN S.ProfitCenterID_Level_04
        WHEN L.ProfitCenterID_Level = 2 THEN S.ProfitCenterID_Level_05
        WHEN L.ProfitCenterID_Level = 3 THEN S.ProfitCenterID_Level_06
        WHEN L.ProfitCenterID_Level = 4 THEN S.ProfitCenterID_Level_07
        --WHEN L.ProfitCenterID_Level = 5 THEN S.ProfitCenterID_Level_08
        --WHEN L.ProfitCenterID_Level = 6 THEN S.ProfitCenterID_Level_09
        --WHEN L.ProfitCenterID_Level = 7 THEN S.ProfitCenterID_Level_10
    END AS [Level 4]
FROM [MyDatabase].[dbo].[Sample] AS S
JOIN [MyDatabase].[dbo].[Sample] AS L
ON 'Business Unit A' = L.ProfitCenterID
WHERE CASE 
        WHEN L.ProfitCenterID_Level = 1 THEN S.ProfitCenterID_Level_01
        WHEN L.ProfitCenterID_Level = 2 THEN S.ProfitCenterID_Level_02
        WHEN L.ProfitCenterID_Level = 3 THEN S.ProfitCenterID_Level_03
        WHEN L.ProfitCenterID_Level = 4 THEN S.ProfitCenterID_Level_04
        WHEN L.ProfitCenterID_Level = 5 THEN S.ProfitCenterID_Level_05
        WHEN L.ProfitCenterID_Level = 6 THEN S.ProfitCenterID_Level_06
        WHEN L.ProfitCenterID_Level = 7 THEN S.ProfitCenterID_Level_07
    END = 'Business Unit A'
...