SQL - Trailing two day average grouped

i have the solution below that works

DECLARE @Sample TABLE ( dt DATE, Total INT );

VALUES  ( '01-Jan-15', 5 ),
        ( '02-Jan-15', 4 ),
        ( '03-Jan-15', 3 ),
        ( '04-Jan-15', 12 ),
        ( '05-Jan-15', 14 ),
        ( '06-Jan-15', 7 ),
        ( '07-Jan-15', 9 );

        ( SELECT    AVG(1.0 * I.Total)
          FROM      @Sample I
          WHERE     I.dt BETWEEN DATEADD(DAY, -1, O.dt) AND O.dt
        ) AS SlidingAverage
FROM    @Sample O

which gives me

02-Jan-15        4.5
03-Jan-15        3.5
04-Jan-15        7.5
05-Jan-15      13
06-Jan-15      10.5
07-Jan-15        8


I need to modify this query to group by Employee to give the trailing two day average.

VALUES  ( '01-Jan-15', 5 , 'Emp A'),
                ( '02-Jan-15', 4 , 'Emp A') ,
               ( '03-Jan-15', 3 , 'Emp A'),
              ( '01-Jan-15', 12 , 'Emp B' ),
              ( '02-Jan-15', 14 , Emp B),
             ( '03-Jan-15', 7 , Emp B),

I will like to get
Emp A    01-Jan-15    
Emp A    02-Jan-15     4.5
Emp A    03-Jan-15     3.5
Emp B    01-Jan-15    
Emp B    02-Jan-15    13
Emp B    03-Jan-15    10.5
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:

      dt DATE ,
      Total INT ,
      EmployeeID INT

VALUES  ( '01-Jan-15', 5, 1 ),
        ( '02-Jan-15', 4, 1 ) ,
        ( '03-Jan-15', 3, 1 ),
        ( '01-Jan-15', 12, 2 ),
        ( '02-Jan-15', 14, 2 ),
        ( '03-Jan-15', 7, 2 );

        ( SELECT    AVG(1.0 * I.Total)
          FROM      @Sample I
          WHERE     I.dt BETWEEN DATEADD(DAY, -1, O.dt) AND O.dt
                    AND I.EmployeeID = O.EmployeeID
        ) AS SlidingAverage
FROM    @Sample O
ORDER BY O.EmployeeID ,
        O.dt ASC;

Open in new window

And for future posts: Please use the embed code buttons in the toolbar.

Also post concise and complete examples. This includes table DDL and sample data INSERT statements as in my example. Please test them before, so that you post valid T-SQL. This makes helping much easier. Thanks for understanding.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.