End Of Year

  • 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

  • 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
  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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[/url]

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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[/url]

  • 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:

    😛

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

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

  • 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[/url]

  • Eugene Elutin (9/20/2012)


    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 problem is that the word can be misunderstood between countries.

    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
  • Luis Cazares (9/20/2012)


    Eugene Elutin (9/20/2012)


    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 problem is that the word can be misunderstood between countries.

    Oh! Just that? Sorry, no problem, it is here: http://dictionary.reference.com/browse/stupid 😉

    _____________________________________________
    "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[/url]

  • Eugene Elutin (9/20/2012)


    Luis Cazares (9/20/2012)


    Eugene Elutin (9/20/2012)


    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 problem is that the word can be misunderstood between countries.

    Oh! Just that? Sorry, no problem, it is here: http://dictionary.reference.com/browse/stupid 😉

    What I meant is that it can be considered more aggresive in some parts than in others. But I agree with your point of view.

    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

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply