Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

End Of Year Expand / Collapse
Author
Message
Posted Tuesday, September 18, 2012 11:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 26, 2013 1:23 PM
Points: 8, Visits: 10
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
Post #1360911
Posted Tuesday, September 18, 2012 11:45 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 3,563, Visits: 7,696
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1360923
Posted Tuesday, September 18, 2012 6:45 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 3,634, Visits: 5,283
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!
Post #1361071
Posted Tuesday, September 18, 2012 7:03 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:35 PM
Points: 1,945, Visits: 2,940
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.

Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1361074
Posted Tuesday, September 18, 2012 8:06 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1361091
Posted Tuesday, September 18, 2012 8:25 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 36,995, Visits: 31,521
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1361098
Posted Wednesday, September 19, 2012 6:20 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
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...



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

How to post your question to get the best and quick help
Post #1361280
Posted Wednesday, September 19, 2012 7:14 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 36,995, Visits: 31,521
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...




--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1361313
Posted Thursday, September 20, 2012 7:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:33 AM
Points: 2,670, Visits: 19,244
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...

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."
Post #1361944
Posted Thursday, September 20, 2012 7:38 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
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...

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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1361967
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse