Compare rows, based on the value (i.e. date and status columns) insert a new row in SQL Server 2012 or SSIS 2012

radical

I need to compare rows in a single table for three different columns "MSName", "MSStatus" and "MSDate". "ID" is the location Identifier and "MSName" is a column for several milestones to be completed on a specific location. Please ignore the "CalcSAED" column for now as I have created it as part of my solution. Please refer to the screenshot below for sample table:

Rules:

Condition 1: IF ( MSName = "FTA" AND MSStatus = "Actual" THEN "INSERT a new row with same column values for ("ID,"SName", "MSStatus", "MSDate") but MSName should "SAEDR",

Condition 2: ELSE IF( MSNAME ="Acc AAA" AND MSStatus ="Actual" AND Condtion 1 = FALSE THEN "INSERT a new row with same column values for ("ID,"SName", "MSStatus", "MSDate") but MSName should "SAEDR",

Condition 3: ELSE IF ( MSNAME ="SAED" AND MSStatus ="Actual" AND Condition 1 AND Condition 2 = FALSE THEN "INSERT a new row with same column values for ("ID,"SName", "MSStatus", "MSDate") but MSName should "SAEDR",

ELSE NEXT

Rules Flow Chart

Sample Table

Sample data

CREATE TABLE D1 (

             ID varchar(2)
            ,SName varchar(50)
            ,MSName varchar(50)
            ,MSStatus Varchar(10)
            ,MSDate Date
            ,CalcSAED varchar(10)
    )

INSERT INTO D1 VALUES ('A1', 'Melbourne', 'FTA', 'Actual', '2016-02-12', NULL)
    INSERT INTO D1 VALUES ('A1', 'Melbourne', 'Acc AAA', 'Pending', '2016-02-13', NULL)
    INSERT INTO D1 VALUES ('A1', 'Melbourne', 'SAED', 'Pending', '2016-02-14', NULL)
    INSERT INTO D1 VALUES ('A2', 'Melbourne1', 'FTA', 'Pending', '2016-02-15', NULL)
    INSERT INTO D1 VALUES ('A2', 'Melbourne1', 'Acc AAA', 'Actual', '2016-02-12', NULL)
    INSERT INTO D1 VALUES ('A2', 'Melbourne1', 'SAED', 'Pending', '2016-02-16', NULL)
    INSERT INTO D1 VALUES ('A3', 'Melbourne2', 'FTA', 'Pending', '2016-02-17', NULL)
    INSERT INTO D1 VALUES ('A3', 'Melbourne2', 'Acc AAA', 'Pending', '2016-02-12', NULL)
    INSERT INTO D1 VALUES ('A3', 'Melbourne2', 'SAED', 'Actual', '2016-02-11', NULL)
    INSERT INTO D1 VALUES ('A4', 'Melbourne3', 'FTA', 'Pending', '2016-02-10', NULL)
    INSERT INTO D1 VALUES ('A4', 'Melbourne3', 'Acc AAA', 'Pending', '2016-02-19', NULL)
    INSERT INTO D1 VALUES ('A4', 'Melbourne3', 'SAED', 'Pending', '2016-02-16', NULL)

    INSERT INTO D1 VALUES ('A5', 'Melbourne4', 'FTA', 'Actual', '2016-02-13', NULL)
    INSERT INTO D1 VALUES ('A5', 'Melbourne4', 'Acc AAA', 'Actual', '2016-02-16', NULL)
    INSERT INTO D1 VALUES ('A5', 'Melbourne4', 'SAED', 'Actual', '2016-02-18', NULL)

    INSERT INTO D1 VALUES ('A6', 'Melbourne5', 'FTA', 'Actual', '2016-02-13', NULL)
    INSERT INTO D1 VALUES ('A6', 'Melbourne5', 'Acc AAA', 'Pending', '2016-02-16', NULL)
    INSERT INTO D1 VALUES ('A6', 'Melbourne5', 'SAED', 'Actual', '2016-02-18', NULL)


    INSERT INTO D1 VALUES ('A7', 'Melbourne6', 'FTA', 'Actual', '2016-02-13', NULL)
    INSERT INTO D1 VALUES ('A7', 'Melbourne6', 'Acc AAA', 'Actual', '2016-02-16', NULL)
    INSERT INTO D1 VALUES ('A7', 'Melbourne6', 'SAED', 'Actual', '2016-02-18', NULL)

My lousy solution so far:

  1. Create a Column called "CalcSAED" and update it as "True" For valid
  2. UNION two select statements for "True" values and change their "MSName" to "SAEDR" and Complete Original table.

PS: I am not even sure if my query is 100% reliable but so far working with the sample table.

My solution code:

UPDATE D1 SET CalcSAED = NULL


    UPDATE D1
       SET CalcSAED = 'True'

      FROM D1 
INNER JOIN D1 AS D2
        ON D2.ID = D1.ID
INNER JOIN D1 AS D3
        ON D1.ID = D3.ID 
     WHERE (D1.MSName LIKE 'FTA' AND D1.MSStatus LIKE 'Actual')
        OR ((D1.MSName LIKE 'Acc AAA' AND D1.MSStatus LIKE 'Actual') AND (D2.MSName LIKE 'FTA' AND D2.MSStatus NOT LIKE 'Actual'))
        OR (D1.MSName LIKE 'SAED' AND D1.MSStatus LIKE 'Actual') AND  (D2.MSName LIKE 'Acc AAA' AND D2.MSStatus NOT LIKE 'Actual') AND (D3.MSName LIKE 'FTA' AND D3.MSStatus NOT LIKE 'Actual') 

GO

    ;WITH TempCTE(ID, SName, MSName, MSStatus, MSDate, CalcSAED) AS

        (


            SELECT ID, SName, MSName = 'SAED-R', MSStatus, MSDate, CalcSAED 
            FROM D1
            WHERE CalcSAED LIKE 'True'

            UNION ALL

            SELECT ID, SName, MSName , MSStatus, MSDate, CalcSAED 
            FROM D1



        )

        SELECT * FROM TempCTE
        ORDER BY ID ASC

Basically, I am using SSIS package to import data from SQL Server database onto a different SQL Server. Currently, I am planning to run this query at the end of SSIS package execution.I am not sure if this is the right way to do it. So, please any suggestions and advice on this will be highly appreciated.. Thanks in advance.

download dowload

In my case i work with the result that you shared in your question the components that i use are :

- OLEDB SOURCE

- MULTICAST

- CONDITIONAL SPLIT

- 3 DERIVED COLUMNS

- UNION ALL

- SORT

- OLEDB DESTINATION

This a picture of your data flow :

enter image description here

In the component Conditional Split i add the 3 condition see the picture as bellow:

enter image description here

Then i add 3 Derived Columns to remplace the value of the row with te value with do you need see the picture as bellow for more details

enter image description here

I did the same thing with all Derived Column

In the end add a sort Component see the picutre as bellow

enter image description here Finaly : add your Destination table and this is end result :

  ID    SName   MSName  MSStatus    MSDate  CalcSAED
A1  Melbourne   Acc AAA Pending 2016-02-13  NULL
A1  Melbourne   FTA Actual  2016-02-12  NULL
A1  Melbourne   SAED    Pending 2016-02-14  NULL
A1  Melbourne   SAEDR   Actual  2016-02-12  NULL
A2  Melbourne1  Acc AAA Actual  2016-02-12  NULL
A2  Melbourne1  FTA Pending 2016-02-15  NULL
A2  Melbourne1  SAED    Pending 2016-02-16  NULL
A2  Melbourne1  SAEDR   Actual  2016-02-12  NULL
A3  Melbourne2  Acc AAA Pending 2016-02-12  NULL
A3  Melbourne2  FTA Pending 2016-02-17  NULL
A3  Melbourne2  SAED    Actual  2016-02-11  NULL
A3  Melbourne2  SAEDR   Actual  2016-02-11  NULL
A4  Melbourne3  Acc AAA Pending 2016-02-19  NULL
A4  Melbourne3  FTA Pending 2016-02-10  NULL
A4  Melbourne3  SAED    Pending 2016-02-16  NULL
A5  Melbourne4  Acc AAA Actual  2016-02-16  NULL
A5  Melbourne4  FTA Actual  2016-02-13  NULL
A5  Melbourne4  SAED    Actual  2016-02-18  NULL
A5  Melbourne4  SAEDR   Actual  2016-02-16  NULL
A6  Melbourne5  Acc AAA Pending 2016-02-16  NULL
A6  Melbourne5  FTA Actual  2016-02-13  NULL
A6  Melbourne5  SAED    Actual  2016-02-18  NULL
A6  Melbourne5  SAEDR   Actual  2016-02-18  NULL
A7  Melbourne6  Acc AAA Actual  2016-02-16  NULL
A7  Melbourne6  FTA Actual  2016-02-13  NULL
A7  Melbourne6  SAED    Actual  2016-02-18  NULL
A7  Melbourne6  SAEDR   Actual  2016-02-18  NULL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert a new row into SQL Server 2012 from an e-mail

From Dev

Compare rows and multiple columns within same table SQL Server 2012

From Dev

SQL Server 2012 : finding the closest date, of muliple columns & rows

From Dev

SSIS 2012 - Insert new rows, ignore existing rows

From Dev

Convert columns to rows in SQL Server 2012

From Dev

Update rows based on rownumber in SQL Server 2012

From Dev

assigning an order ID to different rows of a table based on their Date Order and grouped by CustomerID in SQL Server 2012

From Dev

Adding new column (with inner join) and insert (update) value with case when (SQL Server 2012)

From Dev

How can I turn off identity and insert into a row in SQL Server 2012?

From Dev

How can I turn off identity and insert into a row in SQL Server 2012?

From Dev

How to encrypt certain columns of certain rows in SQL Server 2012

From Dev

Extract date from string and insert into field Microsoft SQL Server 2012

From Dev

selecting random rows with normal distribution based on a column in SQL Server 2012

From Dev

selecting random rows with normal distribution based on a column in SQL Server 2012

From Dev

SQL Server 2012 Date Change

From Dev

Need first record based on the earliest date - Sql Server 2012

From Dev

SQL Server 2012 add value of rows with matching ids

From Dev

SSIS in SQL Server 2012 Foreach Folder

From Dev

SSIS (in SQL Server 2012): Upsert in Lookup component

From Dev

SSIS in SQL Server 2012 Foreach Folder

From Dev

Unable to save SSIS 2015 to SQL Server 2012

From Dev

How to deploy ssis package on SQL Server 2012

From Dev

SQL 2012 Compare dates across several rows

From Dev

Executing SSIS packages from the SSIS catalog on SQL Server 2012

From Dev

Function SELECT that returns a row in SQL SERVER 2012

From Dev

Turning Columns Into Rows Using SQL Server 2012+ AND T-SQL

From Dev

Compare two SQL Server columns with a value and insert output in third column

From Dev

Date conversion issue in SQL Server 2012

From Dev

Sorting by Date of type String in SQL Server 2012

Related Related

  1. 1

    Insert a new row into SQL Server 2012 from an e-mail

  2. 2

    Compare rows and multiple columns within same table SQL Server 2012

  3. 3

    SQL Server 2012 : finding the closest date, of muliple columns & rows

  4. 4

    SSIS 2012 - Insert new rows, ignore existing rows

  5. 5

    Convert columns to rows in SQL Server 2012

  6. 6

    Update rows based on rownumber in SQL Server 2012

  7. 7

    assigning an order ID to different rows of a table based on their Date Order and grouped by CustomerID in SQL Server 2012

  8. 8

    Adding new column (with inner join) and insert (update) value with case when (SQL Server 2012)

  9. 9

    How can I turn off identity and insert into a row in SQL Server 2012?

  10. 10

    How can I turn off identity and insert into a row in SQL Server 2012?

  11. 11

    How to encrypt certain columns of certain rows in SQL Server 2012

  12. 12

    Extract date from string and insert into field Microsoft SQL Server 2012

  13. 13

    selecting random rows with normal distribution based on a column in SQL Server 2012

  14. 14

    selecting random rows with normal distribution based on a column in SQL Server 2012

  15. 15

    SQL Server 2012 Date Change

  16. 16

    Need first record based on the earliest date - Sql Server 2012

  17. 17

    SQL Server 2012 add value of rows with matching ids

  18. 18

    SSIS in SQL Server 2012 Foreach Folder

  19. 19

    SSIS (in SQL Server 2012): Upsert in Lookup component

  20. 20

    SSIS in SQL Server 2012 Foreach Folder

  21. 21

    Unable to save SSIS 2015 to SQL Server 2012

  22. 22

    How to deploy ssis package on SQL Server 2012

  23. 23

    SQL 2012 Compare dates across several rows

  24. 24

    Executing SSIS packages from the SSIS catalog on SQL Server 2012

  25. 25

    Function SELECT that returns a row in SQL SERVER 2012

  26. 26

    Turning Columns Into Rows Using SQL Server 2012+ AND T-SQL

  27. 27

    Compare two SQL Server columns with a value and insert output in third column

  28. 28

    Date conversion issue in SQL Server 2012

  29. 29

    Sorting by Date of type String in SQL Server 2012

HotTag

Archive