Search code examples
sql-servert-sqlsplitsql-server-2017

Achieve the same result from this postgresql query in SQL Server 2017


I have the following query that runs in postgresql-9.6, I need to achieve the same output on a SQL Server DB.

Here is the query, I've replaced all fields from my DB with the string values that would come from them anyway (DB Fields are: "primary_key_fields", "primary_key_values", "table_name", "min_sequence"):

SELECT 
UNNEST(STRING_TO_ARRAY(demo.primary_key_fields, ',')) AS primary_key_fields,    
UNNEST(STRING_TO_ARRAY(demo.primary_key_values, ',')) AS primary_key_values, 
table_name, 
min_sequence, 
ROW_NUMBER() OVER(partition by demo.primary_key_fields) AS rn

FROM (
    SELECT
    'Name,surname,age,location,id' AS primary_key_fields,
    'Nash,Marley,27,South Africa,121' AS primary_key_values,
    'person' AS table_name, 
    '1' AS min_sequence

    UNION ALL
    SELECT
    'Name,surname,age,location,id' AS primary_key_fields,   
    'Paul,Scott,25,South America,999' AS primary_key_values, 
    'person' AS table_name, 
    '1' AS min_sequence
    ) demo

I'm expecting the following output:

Expected Output

Highly appreciate the assistance. I'm using SQL Server 2017.


Solution

  • No longer needed. This question can be closed. No solution was found, changed the source system to accomdate what was needed.