SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Set fiscal year, have year automatically roll over


Set fiscal year, have year automatically roll over

Author
Message
jheltsley
jheltsley
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 8
I would like to (1) set the fiscal year as 12/01 - 11/30 and (2) have the data automatically roll over so only data for the current fiscal year is shown. I have the basic script but I can figure out the rollover. Note the dates are yyyymm.

/*Top 5 Billable Matters*/
select top 5 c.num, m.num, m.name, sum(t.hrs)
from time t
join matter m on t.matter_uno = m.matter_uno
join matter_tm tm on m.matter_uno = tm.matter_uno
join client c on m.client_uno = c.client_uno
join personnel p on t.tk_empl_uno = p.empl_uno
where t.period between '200912' and '201011'
and tm.class in ('B')
and p.employee_num = 'xxxx'
group by c.num, m.num, m.name
order by sum(t.hrs) desc
Ben Teraberry
Ben Teraberry
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1545 Visits: 1199
You're already pulling your weeks from a table "time t". Why not just add a fiscalYear column to that table and group on it and/or just add where t.fiscalYear = whatever?

└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
Conficker
Conficker
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 386
DECLARE @MyDate DATETIME SET @MyDate = getDate()

DECLARE @StartDate DATETIME DECLARE @EndDate DATETIME

SET @StartDate = DATEADD(dd,0, DATEDIFF(dd,0, DATEADD( mm, -(((12 + DATEPART(m, @MyDate)) - 4)%12), @MyDate ) - datePart(d,DATEADD( mm, -(((12 + DATEPART(m, @MyDate)) - 4)%12),@MyDate ))+1 ) ) SET @EndDate = DATEADD(ss,-1,DATEADD(mm,12,@StartDate ))

SELECT @StartDate,@EndDate
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88542 Visits: 41130
I realize that this is an older post and no one may still be listening, but I ran out of "fun" posts and thought I'd give this one a try.

The goal the OP had in mind was to use the existing query but to make it auto-magically lookup the correct fiscal year's worth of information based on the current date. The fiscal year is based on a 12/01 through 11/30 time frame for each year.

The following will do just that. It's a reformatted copy of the original query where I replaced the BETWEEN for t.period with my own devices. The code easily handles leap years and could be turned into a parameterized iTVF (inline Table Valued Function) or maybe even a view, if so desired. If you break them down, the forumlii are pretty easy to understand, as well.


SELECT TOP 5 c.num, m.num, m.name, SUM(t.hrs)
FROM dbo.time t
JOIN dbo.matter m ON t.matter_uno = m.matter_uno
JOIN dbo.matter_tm tm ON m.matter_uno = tm.matter_uno
JOIN dbo.client c ON m.client_uno = c.client_uno
JOIN dbo.personnel p ON t.tk_empl_uno = p.empl_uno
WHERE t.period >= CONVERT(CHAR(6),DATEADD(yy,DATEDIFF(mm,-1,GETDATE())/12 ,0),112)
AND t.period < CONVERT(CHAR(6),DATEADD(yy,DATEDIFF(mm,-1,GETDATE())/12+1,0),112)
AND tm.class IN ('B')
AND p.employee_num = 'xxxx'
GROUP BY c.num, m.num, m.name
ORDER BY SUM(t.hrs) DESC



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search