Need Help--Derived Tables???

  • Hello All!!!

    I need to take diff of two dates,comapring two other columns,row by row.So,I have used Cursor for that,its is working fine...But its taking time.

    I did find tht cursors can be replaced with derived tables.

    I am new to SQL...I m gving the code here

    Please Help!!!

    USE HR_Recruitment_Operational_Reports_StagingDb

    DECLARE @a AS DATETIME ,@B AS DATETIME,@C AS BIGINT,@D AS BIGINT,@Id AS BIGINT

    DECLARE MyCursor CURSOR FOR SELECT MIN(RIV_ID),MAX(RIV_ID),RIV_RCN_ID FROM ABACUSNEWDATA_REC_INTERVIEW GROUP BY RIV_RCN_ID

    OPEN MyCursor

    FETCH NEXT FROM MyCursor

    INTO @C,@D,@Id

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @a = (SELECT RIV_CREATED_ON FROM ABACUSNEWDATA_REC_INTERVIEW WHERE RIV_ID = @C)

    SET @b-2 = (SELECT RIV_CREATED_ON FROM ABACUSNEWDATA_REC_INTERVIEW WHERE RIV_ID = @D)

    print (@Id) print(DATEDIFF(day,@A,@B)) --FROM ABACUS_REC_INTERVIEW --GROUP BY RIV_RCN_ID

    FETCH NEXT FROM MyCursor

    INTO @C,@D,@Id

    END

    CLOSE MyCursor

    DEALLOCATE MyCursor

  • Hello

    This is also untested but should be pretty close to what you're looking for - if it isn't, please post DDL of your table and some sample data.

    [font="Courier New"]SELECT d1.RIV_RCN_ID, DATEDIFF(DAY, d2.RIV_CREATED_ON, d3.RIV_CREATED_ON)

    FROM (SELECT  MIN(RIV_ID) AS MIN_RIV_ID, MAX(RIV_ID) AS MAX_RIV_ID, RIV_RCN_ID

       FROM ABACUSNEWDATA_REC_INTERVIEW

       GROUP BY RIV_RCN_ID) d1

    INNER JOIN ABACUSNEWDATA_REC_INTERVIEW d2 ON d2.RIV_RCN_ID = d1.RIV_RCN_ID AND d2.RIV_ID = d1.MIN_RIV_ID

    INNER JOIN ABACUSNEWDATA_REC_INTERVIEW d3 ON d3.RIV_RCN_ID = d1.RIV_RCN_ID AND d3.RIV_ID = d1.MAX_RIV_ID

    [/font]

    Wandrag - your solution assumes that the min and max RIV_CREATED_ON per RIV_RCN_ID is the same thing as the RIV_CREATED_ON on the min RIV_ID row and max RIV_ID row per RIV_RCN_ID. They may not be the same.

    Cheers

    ChrisM

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I found it myself...Thankss a lot anywayz...

  • bharathi (10/9/2008)


    I found it myself...Thankss a lot anywayz...

    Hi bharathi

    Please would you post your solution? It would finish this thread nicely and also, possibly, provide useful learning material for others.

    Cheers

    ChrisM

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • bharathi (10/9/2008)


    I found it myself...Thankss a lot anywayz...

    Two way street here... would you please post your solution? Thanks. 🙂

    --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.

    Change is inevitable... Change for the better is not.


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

  • Hi Chris!!!

    I could do the same way as wandrag did without derived tables...

    And I tried ur query..it worked perfectly....

    Thanku for ur Help.....

    Regards,

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

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