Monday, March 26, 2012

Running Totals

Hi Folks,

I have a problem that I know that i should be able to code up but have drawn a blank due to it being monday. Anyway.....

Have a table :

create table test_registrations
(
date_maint smalldatetime,
user_name1 varchar (255),
user_address1 varchar (255),
total_users int
)
go

If i have a number of registrations on a particular date then I can tell all how many users have registered in any date by :

select date_maint , count (1)
from test_registrations
group by date_maint
order by date_maint desc
go

The qestion is how can I keep a total registared users count. Say if I have 10 users join on the 1st of Jan and 15 on the 3rd then I want
the total users for the users on 1st to read 10 and total users on the 3rd to read 25.

I know i should be able to code this up but I'm being a dumb ass. Can someone show me a way to code it. Is it some sort of correlated sub query to keep a running total ?select
distinct convert(varchar(10),date_maint,101),
(
select
count(user_name1)
from
test_registrations
where
convert(varchar(10),date_maint,101) <= convert(varchar(10),b.date_maint,101)
)
from
test_registrations b

Try this ... am not sure whether it will work or not ... not near a server right now to test the query|||Excellent mate. Thanks again. I knew it was a subquery.

Cheers|||Is the example above a correlated subquery?

No comments:

Post a Comment