Question

Currently have sql returning a result set as below

WORKFLOWID  UNMATCHEDVALUE  MATCHEDADDRESS  EXCEPTIONREASON
1001        UNIQUE          ADDRESS1        (null)
1001        UNIQUE          ADDRESS2        Some Value

What I am looking for is a result like this

WORKFLOWID UNMATCHEDVALUE MATCHEDADDRESS EXCEPTIONREASON MATCHEDADDRESS2    EXCEPTIONREASON2
 1001      UNIQUE         ADDRESS1       (null)          ADDRESS2           Some Value

So the "variant" columns are MatchedAddress and Exception Reason, the other columns will be the same for each record. Note that for each workflow_id, will always have 2 rows coming back.

I have also created a fiddle to show the schema.

http://sqlfiddle.com/#!6/f7cde/3

Was it helpful?

Solution

Try this:

;WITH CTE AS
(
    SELECT  ws.id as WorkflowStepId,
            ws.workflow_id as WorkflowId, 
            sg.unmatchValue as UnmatchedValue,
            geo_address as MatchedAddress, 
            ws.exception_Value as ExceptionReason,
            ROW_NUMBER() OVER(PARTITION BY ws.workflow_id ORDER BY ws.id) as RN
    FROM workflow_step as ws 
    INNER JOIN workflow as gw 
        ON ws.workflow_id = gw.id
    INNER JOIN super_group as sg 
        ON gw.super_group_id = sg.id
    INNER JOIN alias on 
        ws.id = alias.workflow_step_id
)
SELECT  WorkflowId,
        UnmatchedValue,
        MIN(CASE WHEN RN = 1 THEN MatchedAddress END) MatchedAddress,
        MIN(CASE WHEN RN = 1 THEN ExceptionReason END) ExceptionReason,
        MIN(CASE WHEN RN = 2 THEN MatchedAddress END) MatchedAddress2,
        MIN(CASE WHEN RN = 2 THEN ExceptionReason END) ExceptionReason2
FROM CTE
GROUP BY WorkflowId,
         UnmatchedValue
ORDER BY workflowId

Here is the modified sqlfiddle.

The results are:

╔════════════╦════════════════╦════════════════╦═════════════════╦═════════════════╦══════════════════╗
║ WORKFLOWID ║ UNMATCHEDVALUE ║ MATCHEDADDRESS ║ EXCEPTIONREASON ║ MATCHEDADDRESS2 ║ EXCEPTIONREASON2 ║
╠════════════╬════════════════╬════════════════╬═════════════════╬═════════════════╬══════════════════╣
║       1001 ║ UNIQUE         ║ ADDRESS1       ║ (null)          ║ ADDRESS2        ║ Some Value       ║
╚════════════╩════════════════╩════════════════╩═════════════════╩═════════════════╩══════════════════╝

OTHER TIPS

Try this:

SELECT ws.workflow_id as WorkflowId, sg.unmatchValue as UnmatchedValue,
    MAX(CASE WHEN ws.id = 1 THEN geo_address END) as MatchedAddress1,
    MAX(CASE WHEN ws.id = 2 THEN geo_address END) as MatchedAddress2,
    MAX(CASE WHEN ws.id = 1 THEN ws.exception_Value END) as ExceptionReason1,
    MAX(CASE WHEN ws.id = 2 THEN ws.exception_Value END) as ExceptionReason2 

    FROM workflow_step as ws 
 INNER JOIN workflow as gw on ws.workflow_id = gw.id
    INNER JOIN super_group as sg on gw.super_group_id = sg.id
    inner JOIN alias on ws.id = alias.workflow_step_id
    GROUP BY  ws.workflow_id, sg.unmatchValue

SQL FIDDLE DEMO

Since I can't comment, I just wanted to point out that the answer given by Lamak is using a Common Table Expression. These are generally your best option for solving a recursion problem in sql.

This assumes you only have 2 address types. If you have more I would recommend creating a pivot table.

    select a.*, MATCHEDADDRESS2,EXCEPTIONREASON2
    from
    (Select WORKFLOWID,UNIQUEVALUE,MATCHEDADDRESS,EXCEPTIONREASON
     from "Your Table"
     where MATCHEDADDRESS='ADDRESS1') a

    join

    (Select WORKFLOWID,UNIQUEVALUE,MATCHEDADDRESS as MATCHEDADDRESS2,EXCEPTIONREASON as   XCEPTIONREASON2
     from "Your Table"
     where MATCHEDADDRESS='ADDRESS2') b
          on a.WORKFLOWID=b.WORKFLOWID
               and a.UNMATCHEDVALUE = b.UNMATCHEDVALUE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top