Start Free Trial
Come for the solution, stay for everything else.
Start Free Trial
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 Server
Microsoft SQL Server 2005
Microsoft SQL Server 2008
8/22/2022 - Mon
SELECT t1.id, t1.client_id, t1.visit_date, t1.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
Log in or sign up to see answer
Become an EE member today
7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
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.
See how we're fighting big data
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
That gives me NULL for cumulative hours.
That worked. Ignore my previous comment - my mistake.
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
Plans and Pricing
Certified Expert Program
© 1996-2022 Experts Exchange, LLC. All rights reserved. Covered by US Patent