Avatar of HLRosenberger
HLRosenberger
Flag for United States of America asked on

help with query

I need a query that would cumulatively "count up" the hours for each month.

Data like this:

 
id          client_id           visit_date            visit_hours      
1           10                   1/1/2011 14:00    2.0                  
1           10                   1/21/2011 8:00    5.0                  
1           10                   1/31/2011 9:00    1.0                  
1           10                   2/1/2011 7:00      2.0                  
1            10                  2/25/2011 9:00    3.5                  
 

The resulting data would need to be:
 
id          client_id           visit_date            visit_hours      cumulative_hours        
1           10                   1/1/2011 14:00    2.0                   2.0                                
1           10                   1/21/2011 8:00    5.0                   7.0                          
1           10                   1/31/2011 9:00    1.0                   8.0                          
1           10                   2/1/2011 7:00      2.0                   2.0                            
1            10                  2/25/2011 9:00    3.5                   5.5                            
 
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Patrick Matthews

8/22/2022 - Mon
Patrick Matthews

SELECT t1.id, t1.client_id, t1.visit_date, t1.visit_hours,
    (SELECT SUM(t2.visit_hours)
    FROM SomeTable t2
    WHERE t2.id = t1.id AND t1.client_id = t2.client_id AND YEAR(t2.visit_date) = YEAR(t1.visit_date)
        AND MONTH(t2.visit_date) = MONTH(t1.visit_date)) AS cumulative_hours
FROM SomeTable t1
ORDER BY t1.id, t1.client_id, t1.visit_date
ASKER CERTIFIED SOLUTION
Patrick Matthews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
HLRosenberger

ASKER
That gives me NULL for cumulative hours.
HLRosenberger

ASKER
That worked.  Ignore my previous comment  - my mistake.

Thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Patrick Matthews

:)