I've started to learning T-SQL because I want to get to know ETL tools that I'm using.
Below you can see query from stored procedere that I don't understand in 100%.
I know that it's used to transform data from raw table to output table. I see also that join is used to lookup columns from other tables. But I don't understand part with:
SET R.[Business Posting Group Description] = CASE WHEN (S1.[~Key] IS NOT NULL) THEN S1.[~Value] ELSE R.[Business Posting Group Description] END
Why we use it? Why we have to create [~Key] and [~Value] columns? Can someone explain order of particular steps in this query to me? Thanks in advance.
Full procedure below:
UPDATE R SET R.[Business Posting Group Description] = CASE WHEN (S1.[~Key] IS NOT NULL) THEN S1.[~Value] ELSE R.[Business Posting Group Description] END , R.[Country Name] = CASE WHEN (S2.[~Key] IS NOT NULL) THEN S2.[~Value] ELSE R.[Country Name] END , R.[Salesperson Name] = CASE WHEN (S3.[~Key] IS NOT NULL) THEN S3.[~Value] ELSE R.[Salesperson Name] END FROM [dbo].[Nav_dbo_Customer_R] R INNER JOIN [dbo].[Nav_dbo_Customer_T] T ON T.[DW_Id] = R.[DW_Id] LEFT OUTER JOIN ( SELECT MAX(S.[Description]) AS [~Value] , S.[DW_Account] AS [~Key] , S.[Code] FROM [dbo].[Nav_dbo_Gen. Business Posting Group_V] S GROUP BY S.[DW_Account] , S.[Code] ) S1 ON S1.[~Key] = T.[DW_Account] AND S1.[Code] = T.[Gen. Bus. Posting Group] LEFT OUTER JOIN ( SELECT MAX(S.[Name]) AS [~Value] , S.[DW_Account] AS [~Key] , S.[Code] FROM [dbo].[Nav_dbo_Country/Region_V] S GROUP BY S.[DW_Account] , S.[Code] ) S2 ON S2.[~Key] = T.[DW_Account] AND S2.[Code] = T.[Country/Region Code] LEFT OUTER JOIN ( SELECT MAX(S.[Name]) AS [~Value] , S.[DW_Account] AS [~Key] , S.[Code] FROM [dbo].[Nav_dbo_Salesperson/Purchaser_V] S GROUP BY S.[DW_Account] , S.[Code] ) S3 ON S3.[~Key] = T.[DW_Account] AND S3.[Code] = T.[Salesperson Code]