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


End Of Year


End Of Year

Author
Message
dmarz96
dmarz96
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 26
Good Day To All,
I'm working on a budgetreport that ask the user to enter the up to date.
If the user enters let's say 08/2012 I'm able to get actual data from the begin of the beginning of the year to the end of the month of 08/2012.
My problem is now the user wants to get the budgeted data for the remaining part of the year. For this example I need 09/2012 through 12/2012 budget data.

This is what I have so far but doesn't work properly.

sum(CASE When t.uMonth between dateadd(m,11,dbo.get_boy(dateadd(m, month('#date2#') + 1,'#date2#'),p.hmy)) and dateadd(m,11,dbo.get_boy(dateadd(y,1,'#date2#'),p.hmy))
THEN
CASE a.iNormalBalance WHEN 1
THEN l.dpercent*.01*isnull(-t.sBudget,0)
ELSE l.dpercent*.01*isnull(t.sBudget,0) END
ELSE 0 END ) data8,




***dbo.get_BOY is a function that get Beginning Of Year
*** #date2# ***** is a parameter that is entered by the user
**** data 8 is the placeholder for the end result.
**** a.iNormalBalance is a flag field when 1 do something when it's 0 do something else
*** u.month is the month I test against from the enter parameter
Thanks
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40804 Visits: 19815
Instead of using UDFs (dbo.get_BOY) that will reduce performance, try looking at this article.
http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17267 Visits: 6431
Agreed that the BOY function is a waste of CPU cycles when the conversion is quite easy as follows:


SET DATEFORMAT MDY
SELECT BOY=DATEADD(year, DATEDIFF(year, 0, MyDate), 0)
,EOY=DATEADD(year, 1, DATEADD(year, DATEDIFF(year, 0, MyDate), 0))-1
FROM (SELECT CAST(REPLACE('08/2012','/','-01-') AS DATETIME)) a(MyDate)





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12030 Visits: 5478
CELKO (9/18/2012)
I don't want to post it again, so Google around here for "Report_Periods"; it is a basic look-up table for temporal durations. Don't use UDFs; they screw up the optimizer, cannot port and are a bitch to maintain.


Please ignore this stupid remark!
Although in your case UDF is not really required, they are very helpful and useful where appropriate.
UDF is a powerful feature of SQL Server and when UDFs implemented correctly, they do not screw up the optimiser. If you thinking to port your system into other RDBMS, believe me, having UDFs will not be your main concerns. They are not harder to maintain than stored procedures.
Mr. Celko hates many different features of SQL Server (which he cannot comprehend) and he is very vocal about it...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)

Group: General Forum Members
Points: 209099 Visits: 41973
Eugene Elutin (9/18/2012)
Please ignore this stupid remark!

Easy, big guy. You're right but that's something that he would say. ;-)

--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
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12030 Visits: 5478
Jeff Moden (9/18/2012)
Eugene Elutin (9/18/2012)
Please ignore this stupid remark!

Easy, big guy. You're right but that's something that he would say. ;-)


I've spent 5 minutes to choose the word from the list of "wrong", "irrelevant" and "stupid".
Then tossed my lucky three-side coin and got the one...
Hehe

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)

Group: General Forum Members
Points: 209099 Visits: 41973
Eugene Elutin (9/19/2012)
Jeff Moden (9/18/2012)
Eugene Elutin (9/18/2012)
Please ignore this stupid remark!

Easy, big guy. You're right but that's something that he would say. ;-)


I've spent 5 minutes to choose the word from the list of "wrong", "irrelevant" and "stupid".
Then tossed my lucky three-side coin and got the one...
Hehe

:-P

--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
jcrawf02
jcrawf02
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4796 Visits: 19324
Eugene Elutin (9/19/2012)
Jeff Moden (9/18/2012)
Eugene Elutin (9/18/2012)
Please ignore this stupid remark!

Easy, big guy. You're right but that's something that he would say. ;-)


I've spent 5 minutes to choose the word from the list of "wrong", "irrelevant" and "stupid".
Then tossed my lucky three-side coin and got the one...
Hehe
You should have used a nullable column, and just removed the adjective entirely...

---------------------------------------------------------
How best to post your question
How to post performance problems
Tally Table:What it is and how it replaces a loop

"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12030 Visits: 5478
jcrawf02 (9/20/2012)
Eugene Elutin (9/19/2012)
Jeff Moden (9/18/2012)
Eugene Elutin (9/18/2012)
Please ignore this stupid remark!

Easy, big guy. You're right but that's something that he would say. ;-)


I've spent 5 minutes to choose the word from the list of "wrong", "irrelevant" and "stupid".
Then tossed my lucky three-side coin and got the one...
Hehe
You should have used a nullable column, and just removed the adjective entirely...


Sorry, but it's not nullable in my database. The remark was stupid, why not qualify it as such?
Actually, it was the most polite adjective I could come up with...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
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