Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a stats table that gets written to on a daily basis with the membership totals of my database.Stats TableDateCreated DateTimeMemberStats intI have two questions1) How do I only retrieve the earliest date (1 row per month) for every month. (i.e. should only give my 2004/11/01 not 2004/11/02 for November.2) I need to get the memership growth/decline for each month compared to the previous month. E.g.2004/08/01,36002004/09/01,36122004/10/01,36102004/10/02,3606Must be 2004/08/01,02004/09/01,122004/10/01,-22004/10/02,-4Thanks for the help
MichaelP
Jedi Yak
2489 Posts
Posted - 2004-11-23 : 19:01:46
I'm not sure if this is the solution, but it might point you in the right direction.http://www.sqlteam.com/item.asp?ItemID=3856Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
VIG
Yak Posting Veteran
86 Posts
Posted - 2004-11-23 : 23:57:21
1)
select min(DateCreated) from StatsGROUP BY month(DateCreated)
2)
declare @t table(d datetime,v int)insert @t select '2004/08/01',3600 union allselect'2004/09/01',3612 union allselect '2004/10/01',3610 union allselect '2004/10/02',3606 select d, v-isnull((select top 1 v from @t t1 where t1.d <t.d order by t1.d desc),v)from @t t