How to split different rows into different columns

Zhu, Yueli YZ [NC] 280 Reputation points
2024-04-15T13:14:00.1066667+00:00

I need to retrieve data from azure sql database through ADF. The original table is as following

User's image

I would like to get the output as below

User's image

What kind query can I use in one lookup activity?

Thanks

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Amira Bedhiafi 41,111 Reputation points Volunteer Moderator
    2024-04-15T13:40:28.09+00:00

    You can use the a SQL query to achieve the desired result, I just assumed you know there are at most 2 addresses per id. If there are more, you'll need to extend the pattern:

    SQLCopy

    
    SELECT
    
        id,
    
        MAX(CASE WHEN rn = 1 THEN address END) AS address_1,
    
        MAX(CASE WHEN rn = 2 THEN address END) AS address_2
    
    FROM (
    
        SELECT 
    
            id,
    
            address,
    
            ROW_NUMBER() OVER (PARTITION BY id ORDER BY address) as rn
    
        FROM YourTableName
    
    ) AS sub
    
    GROUP BY id;
    

    Then you can use the above SQL query directly in your ADF Lookup activity :

    • In your pipeline, add a Lookup activity.
    • Configure it to use a dataset linked to your Azure SQL Database.
    • Enter the SQL query as provided above.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.