Been wracking my brains with this

  • Hi Judo,

    Execute the below query, you will be getting the result as you said

    CREATE TABLE #TIMESHEET(TimesheetKey INT, SiteKey INT, Date VARCHAR(10), Duration INT, GroupRule INT)

    INSERT INTO #TIMESHEET VALUES(1000, 1, '7/28/2011', 480, 1)

    INSERT INTO #TIMESHEET VALUES(1001, 1, '7/28/2011', 600, 1)

    INSERT INTO #TIMESHEET VALUES(1002, 1, '7/28/2011', 480, 2)

    SELECT * FROM #TIMESHEET

    SELECT

    B.[TIMESHEETKEY]

    , B.[SITEKEY]

    , B.[DATE]

    , A.[SUM_DURATION]

    , A.[GROUPRULE]

    FROM

    (

    SELECT

    [GroupRule]

    , SUM([DURATION]) [SUM_DURATION] FROM #TIMESHEET

    GROUP BY GroupRule

    ) A

    JOIN #TIMESHEET B

    ON A.[GroupRule] = B.[GroupRule]

    Have a nice day!

  • SELECT TimesheetKey, SiteKey, [Date],

    SUMDuration = SUM(Duration) OVER(PARTITION BY SiteKey, [Date], GroupRule), GroupRule

    FROM #TIMESHEET

    “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

  • SELECT TimesheetKey, SiteKey, [Date],

    SUMDuration = SUM(Duration) OVER(PARTITION BY GroupRule), GroupRule

    FROM #TIMESHEET

    Have a nice day!

  • I forgot to thank you guys. The PARTITION BY worked out greatly!

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

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