Query help

  • jdc

    Old Hand

    Points: 367

    I have to insert data into archive table called Archive_table from Table A based on a date column in Table C.

    Table A is parent and it has child table, Table B and id1 column in Table A is foreign key in Table B.

    Table B has another parent table, Table C where ID2 is foreign key in Table B from Table C.

    Table C has date column Businessday, which I need to use for archiving 90 day old data from Table A and insert into Archive_table.

    Please advise how I can achieve this.

  • rVadim

    Hall of Fame

    Points: 3937

    Something like this:

    INSERT INTO Archive_table (<column_list>)
    SELECT (<column_list>)
    FROM TableA AS A
    INNER JOIN TableB AS B
    ON A.ID1 = B.ID1
    INNER JOIN TableC AS C
    ON B.ID2 = C.ID2
    WHERE C.Businessdate < DATEADD(DAY, -90, CURRENT_TIMESTAMP)

    If you provide DDL (CREATE TABLE) and sample data more people will likely be willing to help.

    --Vadim R.

  • Phil Parkin

    SSC Guru

    Points: 243847

    rVadim wrote:

    Something like this:

    INSERT INTO Archive_table (<column_list>)
    SELECT (<column_list>)
    FROM TableA AS A
    INNER JOIN TableB AS B
    ON A.ID1 = B.ID1
    INNER JOIN TableC AS C
    ON B.ID2 = C.ID2
    WHERE C.Businessdate < DATEADD(DAY, -90, CURRENT_TIMESTAMP)

    If you provide DDL (CREATE TABLE) and sample data more people will likely be willing to help.

    If you want to run this regularly & avoid inserting the same row more than once, it is likely that this code will need to be refined.

    I second the comment about providing DDL & sample data.

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

  • rVadim

    Hall of Fame

    Points: 3937

    Phil Parkin wrote:

    If you want to run this regularly & avoid inserting the same row more than once, it is likely that this code will need to be refined.

    Yes, of course, it's just a basic template. Thank you, Phil.

    --Vadim R.

  • Jeff Moden

    SSC Guru

    Points: 995118

    Your original post clearly defined what you wanted to do.  It also acts as pseudo-code for what needs to be written.  When you run into something like this, at least try the very steps you have written in the form of a SELECT.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems
    Create a Tally Function (fnTally)

  • jdc

    Old Hand

    Points: 367

    Thanks everyone for your valuable inputs.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply