Need to Replicate Rows based on a value

bbmckpt7  于 2023-02-21  发布在  其他
关注(0)|答案(2)|浏览(158)

Using an existing SQL statement, I need to duplicate rows if value in [Action Taken] equals "See Previous Actions".

“See Previous Actions” means refer back to all actions Case Participant received for this case. Basically, each [ViolationCategory] with "See Previous Actions" should be filled in with the same actions as other Categories for this case and participant.

I have two participants in my example as you see by the [CaseParticipantID] one that has [Action Taken] filled in and the other one does not.

I assume...data can be unioned back to itself using Case Number and Case Participant ID, but I do not know how to do that.

Data from SQL Statement (View limited to only pertinent columns for easier reading]
| Case Number | CaseParticipantID | ViolationCategory | Action Taken |
| ------------ | ------------ | ------------ | ------------ |
| 123 | 456 | Conflict of Interest | Compensation |
| 123 | 456 | Conflict of Interest | Reprimand |
| 123 | 456 | Conflict of Interest | Other |
| 123 | 789 | Conflict of Interest | Verbal Reprimand |
| 123 | 456 | Favoritism | See Previous Action |

Desired Outcome

Case NumberCaseParticipantIDViolationCategoryAction Taken
123456Conflict of InterestCompensation
123456Conflict of InterestReprimand
123456Conflict of InterestOther
123789Conflict of InterestVerbal Reprimand
123456FavoritismCompensation
123456FavoritismReprimand
123456FavoritismOther

My current SQL Statement, filtered down to just one case is:

SELECT
     Cases.CaseNumber
    ,Cases.DateOpened
    ,Cases.DateClosed
    ,Participant.CaseParticipantId
    ,Participant.RoleInCase
    ,Participant.RelToOrganization
    ,Participant.Practice
    ,Participant.City
    ,Issues.Issue
    ,Issues.IssueSubCategory
    ,Issues.CaseIssueId
    ,Issues.Outcome
    ,ParticipantAlias.ParticipantName
    ,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN [Warehouse].[Table].[CaseParticipant] as Participant
    ON Cases.CaseNumber = Participant.CaseNumber

LEFT JOIN [Warehouse].[Table].[CaseIssue] as Issues
    ON Participant.CaseParticipantId = Issues.CaseParticipantId

LEFT JOIN [Warehouse].[Table].[CaseAction] as CaseAction
    ON Issues.CaseIssueId = CaseAction.CaseIssueId

LEFT JOIN [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
    ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId

WHERE [Cases].[CaseNumber] = '123'
dxpyg8gm

dxpyg8gm1#

Based on your query, I cannot tell whether it should be a view or procedure, but I'd suggest making the latter. Here is an example of how to achieve the expected output with a stored procedure. The output: Stored Procedure Sample Output

USE [Warehouse]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[SP_GetCaseResult]
    -- Add the parameters for the stored procedure here
    @caseNumber as int
AS
BEGIN
    SET NOCOUNT ON;

    DROP TABLE IF EXISTS #Temp_table
    -- 1. Original query into temp table
    SELECT
    Cases.CaseNumber
    ,Cases.DateOpened
    ,Cases.DateClosed
    ,Participant.CaseParticipantId
    ,Participant.CaseParticipantKey
    ,Participant.ParticipantAliasId
    ,Participant.RoleInCase
    ,Participant.RelToOrganization
    ,Participant.Practice
    ,Participant.City
    ,Issues.Issue
    ,Issues.IssueSubCategory
    ,Issues.CaseIssueId
    ,Issues.Outcome
    ,ParticipantAlias.ParticipantName
    ,CaseAction.ActionTaken
    INTO #Temp_table
    FROM [Warehouse].[Table].[Case] as Cases
    LEFT JOIN
    [Warehouse].[Table].[CaseParticipant] as Participant
    ON Cases.CaseNumber = Participant.CaseNumber
    LEFT JOIN
    [Warehouse].[Table].[CaseIssue] as Issues
    ON Participant.CaseParticipantId = Issues.CaseParticipantId
    LEFT JOIN
    [Warehouse].[Table].[CaseAction] as CaseAction
    ON Issues.CaseIssueId = CaseAction.CaseIssueId
    LEFT JOIN
    [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
    ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
    WHERE [Cases].[CaseNumber] = @caseNumber

    -- 2. Query from temptable without "See previous actions"
    SELECT * FROM #Temp_table t1 WHERE t1.ActionTaken <> 'See Previous Actions'

    --  "Joining" the two results
    UNION ALL

    -- 3. Query from temptable with joining the two table with and without "See previous actions" and getting needed output.
    SELECT
         t1.CaseNumber
        ,t1.DateOpened
        ,t1.DateClosed
        ,t1.CaseParticipantId
        ,t1.CaseParticipantKey
        ,t1.ParticipantAliasId
        ,t1.RoleInCase
        ,t1.RelToOrganization
        ,t1.Practice
        ,t1.City
        ,t1.Issue
        ,t1.IssueSubCategory
        ,t1.CaseIssueId
        ,t1.Outcome
        ,t1.ParticipantName
        ,t2.ActionTaken
    FROM #Temp_table t2
    INNER JOIN #Temp_table t1 ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
    WHERE t1.ActionTaken = 'See Previous Actions' AND t2.ActionTaken <> 'See Previous Actions'

END
GO

OR If you really want it without stored-procedure: Here is an example for it:

Example of simple query

DECLARE @caseNumber int = '123'

SELECT DISTINCT 
     t1.CaseNumber
    ,t1.DateOpened
    ,t1.DateClosed
    ,t1.CaseParticipantId
    ,t1.CaseParticipantKey
    ,t1.ParticipantAliasId
    ,t1.RoleInCase
    ,t1.RelToOrganization
    ,t1.Practice
    ,t1.City
    ,t1.Issue
    ,t1.IssueSubCategory
    ,t1.CaseIssueId
    ,t1.Outcome
    ,t1.ParticipantName
    ,t2.ActionTaken
FROM (
    SELECT
    Cases.CaseNumber
    ,Cases.DateOpened
    ,Cases.DateClosed
    ,Participant.CaseParticipantId
    ,Participant.CaseParticipantKey
    ,Participant.ParticipantAliasId
    ,Participant.RoleInCase
    ,Participant.RelToOrganization
    ,Participant.Practice
    ,Participant.City
    ,Issues.Issue
    ,Issues.IssueSubCategory
    ,Issues.CaseIssueId
    ,Issues.Outcome
    ,ParticipantAlias.ParticipantName
    ,CaseAction.ActionTaken
    FROM [Warehouse].[Table].[Case] as Cases
    LEFT JOIN
    [Warehouse].[Table].[CaseParticipant] as Participant
    ON Cases.CaseNumber = Participant.CaseNumber
    LEFT JOIN
    [Warehouse].[Table].[CaseIssue] as Issues
    ON Participant.CaseParticipantId = Issues.CaseParticipantId
    LEFT JOIN
    [Warehouse].[Table].[CaseAction] as CaseAction
    ON Issues.CaseIssueId = CaseAction.CaseIssueId
    LEFT JOIN
    [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
    ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
    WHERE [Cases].[CaseNumber] = @caseNumber --123
) t1
FULL OUTER JOIN (
    SELECT
    Cases.CaseNumber
    ,Cases.DateOpened
    ,Cases.DateClosed
    ,Participant.CaseParticipantId
    ,Participant.CaseParticipantKey
    ,Participant.ParticipantAliasId
    ,Participant.RoleInCase
    ,Participant.RelToOrganization
    ,Participant.Practice
    ,Participant.City
    ,Issues.Issue
    ,Issues.IssueSubCategory
    ,Issues.CaseIssueId
    ,Issues.Outcome
    ,ParticipantAlias.ParticipantName
    ,CaseAction.ActionTaken
    FROM [Warehouse].[Table].[Case] as Cases
    LEFT JOIN
    [Warehouse].[Table].[CaseParticipant] as Participant
    ON Cases.CaseNumber = Participant.CaseNumber
    LEFT JOIN
    [Warehouse].[Table].[CaseIssue] as Issues
    ON Participant.CaseParticipantId = Issues.CaseParticipantId
    LEFT JOIN
    [Warehouse].[Table].[CaseAction] as CaseAction
    ON Issues.CaseIssueId = CaseAction.CaseIssueId
    LEFT JOIN
    [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
    ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
    WHERE [Cases].[CaseNumber] = @caseNumber --123 
) t2 
    ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
    WHERE t2.ActionTaken <> 'See Previous Actions'
8ehkhllq

8ehkhllq2#

Here is the UNION sample also:

However considering performance, if your tables contain hundreds of thousands of records, then the stored procedure is the "fastest" as it only queries the main data once ! (afterwards the temporary tables are in use).

In the other 2 examples, the more you query the tables (which contains many data) the less the performance will be. There won't be much difference if the tables don't contain many data inside. So it's up to you what way method you'd choose getting the expected output.

UNION example result

DECLARE @caseNumber int = '123'
SELECT
    Cases.CaseNumber
    ,Cases.DateOpened
    ,Cases.DateClosed
    ,Participant.CaseParticipantId
    ,Participant.CaseParticipantKey
    ,Participant.ParticipantAliasId
    ,Participant.RoleInCase
    ,Participant.RelToOrganization
    ,Participant.Practice
    ,Participant.City
    ,Issues.Issue
    ,Issues.IssueSubCategory
    ,Issues.CaseIssueId
    ,Issues.Outcome
    ,ParticipantAlias.ParticipantName
    ,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
    [Warehouse].[Table].[CaseParticipant] as Participant
    ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
    [Warehouse].[Table].[CaseIssue] as Issues
    ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
    [Warehouse].[Table].[CaseAction] as CaseAction
    ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
    [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
    ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken <> 'See Previous Actions'

UNION ALL

SELECT DISTINCT 
     t1.CaseNumber
    ,t1.DateOpened
    ,t1.DateClosed
    ,t1.CaseParticipantId
    ,t1.CaseParticipantKey
    ,t1.ParticipantAliasId
    ,t1.RoleInCase
    ,t1.RelToOrganization
    ,t1.Practice
    ,t1.City
    ,t1.Issue
    ,t1.IssueSubCategory
    ,t1.CaseIssueId
    ,t1.Outcome
    ,t1.ParticipantName
    ,t2.ActionTaken
FROM (
    SELECT
    Cases.CaseNumber
    ,Cases.DateOpened
    ,Cases.DateClosed
    ,Participant.CaseParticipantId
    ,Participant.CaseParticipantKey
    ,Participant.ParticipantAliasId
    ,Participant.RoleInCase
    ,Participant.RelToOrganization
    ,Participant.Practice
    ,Participant.City
    ,Issues.Issue
    ,Issues.IssueSubCategory
    ,Issues.CaseIssueId
    ,Issues.Outcome
    ,ParticipantAlias.ParticipantName
    ,CaseAction.ActionTaken
    FROM [Warehouse].[Table].[Case] as Cases
    LEFT JOIN
    [Warehouse].[Table].[CaseParticipant] as Participant
    ON Cases.CaseNumber = Participant.CaseNumber
    LEFT JOIN
    [Warehouse].[Table].[CaseIssue] as Issues
    ON Participant.CaseParticipantId = Issues.CaseParticipantId
    LEFT JOIN
    [Warehouse].[Table].[CaseAction] as CaseAction
    ON Issues.CaseIssueId = CaseAction.CaseIssueId
    LEFT JOIN
    [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
    ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
    WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken = 'See Previous Actions'
    ) t1
    INNER JOIN (
        SELECT
            Cases.CaseNumber
            ,Cases.DateOpened
            ,Cases.DateClosed
            ,Participant.CaseParticipantId
            ,Participant.CaseParticipantKey
            ,Participant.ParticipantAliasId
            ,Participant.RoleInCase
            ,Participant.RelToOrganization
            ,Participant.Practice
            ,Participant.City
            ,Issues.Issue
            ,Issues.IssueSubCategory
            ,Issues.CaseIssueId
            ,Issues.Outcome
            ,ParticipantAlias.ParticipantName
            ,CaseAction.ActionTaken
        FROM [Warehouse].[Table].[Case] as Cases
        LEFT JOIN
            [Warehouse].[Table].[CaseParticipant] as Participant
            ON Cases.CaseNumber = Participant.CaseNumber
        LEFT JOIN
            [Warehouse].[Table].[CaseIssue] as Issues
            ON Participant.CaseParticipantId = Issues.CaseParticipantId
        LEFT JOIN
            [Warehouse].[Table].[CaseAction] as CaseAction
            ON Issues.CaseIssueId = CaseAction.CaseIssueId
        LEFT JOIN
            [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
            ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
        WHERE [Cases].[CaseNumber] = @caseNumber AND [CaseAction].ActionTaken <> 'See Previous Actions'
    ) t2 
    ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId

相关问题