Tune the Stored Procedure

  • I want to combine the below mention two Update statements to one statement.

    How is it possible. somebody please help.

    Thanks,

    CREATE TABLE #Temp2(PRID INT,LOCID INT,Rev_CatID INT,MSTTYPE CHAR(1),

    AGGTracked INT,AGGDE1 INT,AGGDE2 INT,AGGUpdateEntry INT,AGGPagesReviwed INT,AGGSenttoImageReview INT,

    AGGReplaced INT,AGGDuplicate INT,AGGReIndexed INT,AGGDeleted INT)

    --Inserting into Temp table PRID,LocID,Rev_CatID,MSTType remaining all with '0'

    INSERT INTO #Temp2

    SELECT PRID,LOCID,Rev_CatID,MSTTYPE,0,0,0,0,0,0,0,0,0,0

    FROM #TEMP T

    GROUP BY T.PRID, T.LOCID,T.Rev_CatID, T.MSTTYPE

    ORDER BY T.PRID, T.LOCID,T.Rev_CatID

    --Updating the Temp table of AGGTracked Column with locID is null

    UPDATE #TEMP2 SET AGGTracked=A.AGGTracked FROM

    (SELECT COUNT(PRID) as AGGTracked,PRID,LOCID,Rev_CatID,MSTTYPE FROM #TEMP

    WHERE MCMetric ='New' AND PEEvent = 'New'

    group by PRID,LOCID,Rev_CatID,MSTTYPE) A

    INNER JOIN #TEMP2 B ON A.PRID = B.PRID AND A.LOCID is null and b.locid is null AND A.Rev_CatID = B.Rev_CatID AND A.MSTTYPE = B.MSTTYPE

    --Updating the Temp table of AGGTracked Column with locID is not null

    UPDATE #TEMP2 SET AGGTracked=A.AGGTracked FROM

    (SELECT COUNT(PRID) as AGGTracked,PRID,LOCID,Rev_CatID,MSTTYPE FROM #TEMP

    WHERE MCMetric ='New' AND PEEvent = 'New'

    group by PRID,LOCID,Rev_CatID,MSTTYPE) A

    INNER JOIN #TEMP2 B ON A.PRID = B.PRID AND A.LOCID=B.LOCID AND A.Rev_CatID = B.Rev_CatID AND A.MSTTYPE = B.MSTTYPE

  • Tried this?:

    UPDATE #TEMP2 SET AGGTracked=A.AGGTracked FROM

    (SELECT COUNT(PRID) as AGGTracked,PRID,LOCID,Rev_CatID,MSTTYPE FROM #TEMP

    WHERE MCMetric ='New' AND PEEvent = 'New'

    group by PRID,LOCID,Rev_CatID,MSTTYPE) A

    INNER JOIN #TEMP2 B ON A.PRID = B.PRID AND (A.LOCID=B.LOCID OR (A.LOCID is null and b.locid is null)) AND A.Rev_CatID = B.Rev_CatID AND A.MSTTYPE = B.MSTTYPE

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Did you try this?

    CREATE TABLE #Temp2(PRID INT,LOCID INT,Rev_CatID INT,MSTTYPE CHAR(1),

    AGGTracked INT,AGGDE1 INT,AGGDE2 INT,AGGUpdateEntry INT,AGGPagesReviwed INT,AGGSenttoImageReview INT,

    AGGReplaced INT,AGGDuplicate INT,AGGReIndexed INT,AGGDeleted INT)

    INSERT INTO #Temp2

    SELECT PRID,LOCID,Rev_CatID,MSTTYPE,

    SUM(CASE WHEN MCMetric ='New' AND PEEvent = 'New' THEN 1 ELSE 0 END) AS AGGTracked

    ,0,0,0,0,0,0,0,0,0

    FROM #TEMP T

    GROUP BY T.PRID, T.LOCID,T.Rev_CatID, T.MSTTYPE

    ORDER BY T.PRID, T.LOCID,T.Rev_CatID

  • Thank you for your reply,

    This issue has been solved.

  • Your issue may have been resolved but there is something that no one has pointed out yet. Do NOT do this, as it is work for no purpose:

    --Inserting into Temp table PRID,LocID,Rev_CatID,MSTType remaining all with '0'

    INSERT INTO #Temp2

    SELECT PRID,LOCID,Rev_CatID,MSTTYPE,0,0,0,0,0,0,0,0,0,0

    FROM #TEMP T

    GROUP BY T.PRID, T.LOCID,T.Rev_CatID, T.MSTTYPE

    ORDER BY T.PRID, T.LOCID,T.Rev_CatID

    Inserting into a table with an order by causes a sort but does NOT help anything that happens with that data. If the table doesn't have a clustered index on the order of the order by or an identity column where you need the identity order to be specific you have wasted effort here.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Thanks for your suggestion.

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

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