Как получить разные IBAN для каждого типа для сотрудника - PullRequest
0 голосов
/ 23 сентября 2019

У меня следующий запрос, который возвращает мне другой IBAN для EmployeeId = 54:

WITH cte AS ( SELECT TOP 1000 [E].[EmployeeId]
          ,(CASE WHEN BIT.Label='Salary' THEN [BI].[IBAN] END) AS IBAN_Salary
          ,(CASE WHEN BIT.Label='Expenses Claims' THEN [BI].[IBAN] END) AS IBAN_Expenses_Claims
          ,bi.UpdatedDate
         , ROW_NUMBER() OVER (PARTITION BY E.EmployeeId,bit.Label ORDER BY BI.UpdatedDate DESC) rn
           FROM [Employee] AS [E]
           LEFT JOIN [AF_AdminFile] AS [AAF]  ON [AAF].[AdminFileId] = [E].[AdminFileId]
           LEFT JOIN [AF_BankInformation] AS [AFB] ON [AFB].[AdminFileId] = [AAF].[AdminFileId]
           LEFT JOIN [BankInformation] AS [BI] ON [BI].[BankInformationId] = [AFB].[BankInformationId]
           LEFT JOIN [BankInformationType] AS [BIT] ON [BIT].[BankInformationTypeId] = [AFB].[BankInformationTypeId]
           LEFT JOIN [BankInformationSystem] AS [BIS] ON [BIS].[BankInformationSystemId]=[BI].[BankInformationSystemId]
           LEFT JOIN [Currency] AS [Cur] ON [Cur].[ID]=[BI].[CurrencyId]
           WHERE BIT.BankInformationTypeId IN (1,2) 
           AND e.EmployeeId=54
           GROUP BY e.EmployeeId, BIT.Label,BI.UpdatedDate,BI.IBAN
           ORDER BY e.EmployeeId 
           )

       SELECT * FROM cte WHERE rn =1

Вывод предыдущего запроса:

enter image description here

Как получить отдельный столбец для IBAN_Salary, IBAN_Expenses_Claims, updatedDated_IBAN_Salary, UpdatesDated_IBAN_Expenses_Claims в той же строке, как показано ниже?:

EmployeeId IBAN_Salary IBAN_Expenses_Claims UpdatedDated_IBAN_Salary   UpdatedDated_IBAN_Expenses_Claims
54         lorem ipsum lorem ipsum          2019-08-09 09:38:00.7350889 2018-07-07 17:09:16.7598906

Ответы [ 2 ]

1 голос
/ 23 сентября 2019

Используйте тот же CASE для дат.Агрегируйте по ключу

 WITH cte AS ( SELECT TOP 1000 [E].[EmployeeId]
      ,(CASE WHEN BIT.Label='Salary' THEN [BI].[IBAN] END) AS IBAN_Salary
      ,(CASE WHEN BIT.Label='Expenses Claims' THEN [BI].[IBAN] END) AS IBAN_Expenses_Claims
      ,(CASE WHEN BIT.Label='Salary' THEN bi.UpdatedDate END) AS UpdatedDated_IBAN_Salary
      ,(CASE WHEN BIT.Label='Expenses Claims' THEN bi.UpdatedDate END) AS UpdatedDated_IBAN_Expenses_Claims
     , ROW_NUMBER() OVER (PARTITION BY E.EmployeeId,bit.Label ORDER BY BI.UpdatedDate DESC) rn
       FROM [Employee] AS [E]
       LEFT JOIN [AF_AdminFile] AS [AAF]  ON [AAF].[AdminFileId] = [E].[AdminFileId]
       LEFT JOIN [AF_BankInformation] AS [AFB] ON [AFB].[AdminFileId] = [AAF].[AdminFileId]
       LEFT JOIN [BankInformation] AS [BI] ON [BI].[BankInformationId] = [AFB].[BankInformationId]
       LEFT JOIN [BankInformationType] AS [BIT] ON [BIT].[BankInformationTypeId] = [AFB].[BankInformationTypeId]
       LEFT JOIN [BankInformationSystem] AS [BIS] ON [BIS].[BankInformationSystemId]=[BI].[BankInformationSystemId]
       LEFT JOIN [Currency] AS [Cur] ON [Cur].[ID]=[BI].[CurrencyId]
       WHERE BIT.BankInformationTypeId IN (1,2) 
       AND e.EmployeeId=54
       GROUP BY e.EmployeeId, BIT.Label,BI.UpdatedDate,BI.IBAN
       ORDER BY e.EmployeeId 
       )

   SELECT [EmployeeId]
        , max(IBAN_Salary) IBAN_Salary
        , max(IBAN_Expenses_Claims) IBAN_Expenses_Claims
        , max(UpdatedDated_IBAN_Salary) UpdatedDated_IBAN_Salary
        , max(UpdatedDated_IBAN_Expenses_Claims) UpdatedDated_IBAN_Expenses_Claims
  FROM cte WHERE rn =1
  GROUP BY [EmployeeId];

Глядя на ваш пример результата, это, вероятно, можно сделать за один шаг.

1 голос
/ 23 сентября 2019

Один из подходов, возможно, не оптимальный, состоит в том, чтобы просто выполнить другую агрегацию на CTE:

SELECT
    EmployeeId,
    MAX(IBAN_Salary) AS IBAN_Salary,
    MAX(IBAN_Expenses_Claims) AS IBAN_Expenses_Claims,
    MAX(CASE WHEN IBAN_Salary IS NOT NULL THEN UpdatedDated END) AS UpdatedDated_IBAN_Salary,
    MAX(CASE WHEN IBAN_Expenses_Claims IS NOT NULL THEN UpdatedDated END) AS UpdatedDated_IBAN_Expenses_Claims
FROM cte
WHERE
    rn = 1    -- uncomment for all employees
GROUP BY
    EmployeeId;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...