How to Pass date dynamically ...

  • I have an table which contains lots of transaction with a column called Validity which is a boolean(True or False).

    Now i need to write a query whereby a user gives a fromdate and a todate

    for ex: Jan 1 2013 to Jan 31 2013 (Thru reporting SSRS)

    On Jan 1 2013...the query should check to see the last validity record from the table and give me some value

    on jan 2 it should give the last validity value ...so on until 31 jan

    for ex on Jan 1 2013 the last validity was on July 20 1994 then thats the value i want

    For 2nd Jan 2013 if there was validity on 1 jan 2013 then i need that value for a particular item.

    so my prob is how to pass the from and the todate

    in the query which would bring me the last validity

    so the result i am looing for is

    Jan 1 2013 100

    Jan 2 2013 200

    Jan 3 2013 200 (Because there was no validity)

    Jan 4 2013 200 (same...because the last validity was the same of jan 2 2013)

    Jan 5 2013 300 (Because on Jan 4th 2013 there was validiy done)

    Jan 6th 2013 300

    so on untill jan 31st

    Any help on how to make this query work

  • In order to help you, you're going to need to help us by following best practices for posting a question of this type:

    - Provide DDL (CREATE TABLE)

    - Provide consumable sample data (i.e., INSERTs)

    - Expected results from the sample data

    This article by Jeff Moden can help you to understand:

    Forum Etiquette: How to post data/code on a forum to get the best help [/url]


    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

  • Yeah, you definitely need to add more details here. For example, you state that you are trying to find the last "validity" situation for each date in the specified range. However, you don't quite state exactly what that validity really means in an easily understood way. You provide an example that says that for a particular date, that July 20 1994 was the last validity for that date, but then provide example data that provides values of 100, 200, and 300 as the desired output values, with no obvious connection between the 7/20/1994 date and any of the actual output values. Thus, without table structures and some kind of sample data to work with, along with an accurate set of the exact rules that need to be observed, providing assistance would be akin to trying to measure the distance to the moon or the Sun with only the technology available to early humans from a million years ago, and without any knowledge they didn't have.

    However, as you asked about passing a date range, I suspect you may have had difficulty figuring out how to get that date range expanded into all the individual dates. Here's a way to do that:

    DECLARE @START_DATE AS date = '01/01/2013',

    @END_DATE AS date = '01/31/2013'

    ;WITH cteDateValues AS (

    SELECT 0 AS N, @START_DATE AS DATE_VALUE

    UNION ALL

    SELECT N + 1, CAST(DATEADD(dd, N + 1, @START_DATE) AS date)

    FROM cteDateValues

    WHERE N < DATEDIFF(dd, @START_DATE, @END_DATE)

    )

    SELECT DATE_VALUE

    FROM cteDateValues

    OPTION (MAXRECURSION 1000)

    This code, if run, will produce a table containing all the dates in the range in table form, such that additional common table expressions could then make use of that table. You could pass the two dates as parameters to a stored procedure, and use the procedures variables instead of explicitly declaring them. Of course, going from having the list of dates to your final end result is still a bit of a question mark, as we don't have enough information to go on. FYI, that OPTION (MAXRECURSION 1000) is just a way to prevent the recursion taking place in the CTE from going into an infinite loop, by limiting the number of values to 1000. You can change that value to any integer less than 32,768, or to 0 to remove any limitation on the number of records coming out (not generally recommended). In this case, 1000 would give you the ability to specify a date range almost 3 years long.

    Does that help get you started?

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • sgmunson (12/16/2013)


    Yeah, you definitely need to add more details here. For example, you state that you are trying to find the last "validity" situation for each date in the specified range. However, you don't quite state exactly what that validity really means in an easily understood way. You provide an example that says that for a particular date, that July 20 1994 was the last validity for that date, but then provide example data that provides values of 100, 200, and 300 as the desired output values, with no obvious connection between the 7/20/1994 date and any of the actual output values. Thus, without table structures and some kind of sample data to work with, along with an accurate set of the exact rules that need to be observed, providing assistance would be akin to trying to measure the distance to the moon or the Sun with only the technology available to early humans from a million years ago, and without any knowledge they didn't have.

    However, as you asked about passing a date range, I suspect you may have had difficulty figuring out how to get that date range expanded into all the individual dates. Here's a way to do that:

    DECLARE @START_DATE AS date = '01/01/2013',

    @END_DATE AS date = '01/31/2013'

    ;WITH cteDateValues AS (

    SELECT 0 AS N, @START_DATE AS DATE_VALUE

    UNION ALL

    SELECT N + 1, CAST(DATEADD(dd, N + 1, @START_DATE) AS date)

    FROM cteDateValues

    WHERE N < DATEDIFF(dd, @START_DATE, @END_DATE)

    )

    SELECT DATE_VALUE

    FROM cteDateValues

    OPTION (MAXRECURSION 1000)

    This code, if run, will produce a table containing all the dates in the range in table form, such that additional common table expressions could then make use of that table. You could pass the two dates as parameters to a stored procedure, and use the procedures variables instead of explicitly declaring them. Of course, going from having the list of dates to your final end result is still a bit of a question mark, as we don't have enough information to go on. FYI, that OPTION (MAXRECURSION 1000) is just a way to prevent the recursion taking place in the CTE from going into an infinite loop, by limiting the number of values to 1000. You can change that value to any integer less than 32,768, or to 0 to remove any limitation on the number of records coming out (not generally recommended). In this case, 1000 would give you the ability to specify a date range almost 3 years long.

    Does that help get you started?

    Ah... be careful, now. That uses an rCTE that counts and even small trips to such a thing can cause real performance and resource usage problems. Please see the following article for more information.

    http://www.sqlservercentral.com/articles/T-SQL/74118/

    Instead, I recommend the use of a Calendar Table or a Tally Table or one of Itzik Ben-Gan's cCTE (cascading CTE) methods.

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

Viewing 4 posts - 1 through 3 (of 3 total)

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