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 |
|
dotinf
Starting Member
6 Posts |
Posted - 2009-12-31 : 20:14:53
|
Hi guys, this is the first time I have attempted date functions in SQL. I have tried many ways to solve a little issue I have with no success, so I thought I'd go back to basics and ask for help.I have a simple table with 2 columns, one a date column and one a number column. I am trying to group the results into previous financial years without knowing what the starting year is. The following is a simple select statement and as you can see it is very simple, but I have not had any success with grouping. I am using SQL Server 2005 and the financial years start on the 1st of July and finish on the 30th of June.SELECT ReadingDate, ReadingValueFROM tblReadings Results:ReadingDate ReadingValue20/02/2006 208/04/2006 4030/08/2006 4010/01/2007 2214/01/2007 216/04/2007 818/08/2007 1220/12/2007 802/02/2008 682/04/2008 620/08/2008 768/12/2008 81/01/2009 24/04/2009 812/10/2009 1612/12/2009 12Thanks guys |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-01-01 : 05:14:33
|
| set dateformat dmydeclare @tbl as table(value int,dates datetime)insert into @tblselect 20,'20/02/2006' union allSelect 40,'8/04/2006' union allSelect 40,'30/08/2006' union allSelect 22,'10/01/2007' union allSelect 2,'14/01/2007' union allSelect 8,'16/04/2007' union allSelect 12,'16/04/2007' union allSelect 80,'20/12/2007' union allSelect 68,'2/02/2008' union allSelect 6,'2/04/2008' union allSelect 76,'20/08/2008' union allSelect 8,'8/12/2008' union allSelect 2,'1/01/2009' union allSelect 8,'4/04/2009' union allSelect 16,'12/10/2009' union allSelect 12,'12/12/2009' select * from @tblselect convert(varchar,yr) + '-' + convert(varchar,yr+1) as FinancialYear,value from(select DATEPART(yy,dateadd(mm,-7,dates))as yr,SUM(value) as value from @tblgroup by DATEPART(yy,dateadd(mm,-7,dates)))tPBUH |
 |
|
|
|
|
|
|
|