Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Pass date dynamically ...


How to Pass date dynamically ...

Author
Message
jagan 68202
jagan 68202
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: 5
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
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4241 Visits: 6431
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


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
sgmunson
sgmunson
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2830 Visits: 3745
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)
:-):-):-)
Health & Nutrition
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44871 Visits: 39850
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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