Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dgaylor
Yak Posting Veteran
54 Posts |
Posted - 2005-09-20 : 14:12:35
|
| I have a table that has registration data in it. I need to return the cumulative head-count for each day before the event starts.I currently have a select statement that is returning the individual counts per day from event:DaysFromEvent HeadCount46 145 244 3 But what I really need is:DaysFromEvent HeadCount46 145 344 6Can anyone help? Thanks in advanceAlso, when the number of days from the event is greater than 60 then I need those day counts to be rolled up into one value, so I need a 60+ DaysFromEvent value.-------------------------------------------------------------------CREATE TABLE [dbo].[Reg] ( [RegDate] [datetime] NOT NULL , [Paid] [bit] NOT NULL ) GOINSERT reg (regdate, paid) VALUES ('2005-09-17', 0)INSERT reg (regdate, paid) VALUES ('2005-09-17', 1)INSERT reg (regdate, paid) VALUES ('2005-09-18', 0)INSERT reg (regdate, paid) VALUES ('2005-09-18', 1)INSERT reg (regdate, paid) VALUES ('2005-09-18', 1)INSERT reg (regdate, paid) VALUES ('2005-09-19', 1)INSERT reg (regdate, paid) VALUES ('2005-09-19', 1)INSERT reg (regdate, paid) VALUES ('2005-09-19', 1)GOSELECT DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) as DaysFromEvent, COUNT(*) As HeadCountFROM RegWHERE Reg.Paid = 1 AND (DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) >= 0)GROUP BY DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) ORDER BY DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) descGO |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-21 : 01:25:23
|
| Do you want to show this in your Presentation layer like Reports?If so, use RunningTotal feature of Reports. You can do this with sql query but if there are thousands of data you will get performance issuesHere is an sql querySelect DaysFromEvent (Select sum(HeadCount) from yourTable where DaysFromEvent <=T.DaysFromEvent)from yourTable TMadhivananFailing to plan is Planning to fail |
 |
|
|
dgaylor
Yak Posting Veteran
54 Posts |
Posted - 2005-09-21 : 01:59:57
|
| Hi, this is something I would rather have sql return, if possible, rather than handling it in the presentation layer. I am not sure I understand the suggested sql code, it doesn't use my table structure. Thanks. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-21 : 02:12:07
|
>>if possible, rather than handling it in the presentation layer. No you should consider doing this in your presentation layerI just gave an idea of how to do Running Total thru queryWhat you need is something like thisSelect * into #t from ( SELECT DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) as DaysFromEvent, COUNT(*) As HeadCount FROM Reg WHERE Reg.Paid = 1 AND (DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) >= 0) GROUP BY DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) ORDER BY DATEDIFF(dd, Reg.RegDate, cast('11/2/2005' as datetime) ) desc ) TSelect DaysFromEvent (Select sum(HeadCount) from #t where DaysFromEvent <=T.DaysFromEvent)from #t TMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|