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

How to Pass date dynamically ... Expand / Collapse
Author
Message
Posted Wednesday, December 11, 2013 3:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 5:21 AM
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
Post #1521805
Posted Wednesday, December 11, 2013 6:08 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: Yesterday @ 3:25 AM
Points: 3,417, Visits: 5,328
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!
Post #1522113
Posted Monday, December 16, 2013 7:46 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, September 8, 2014 4:07 PM
Points: 1,669, Visits: 2,215
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)

Internet ATM Machine
Post #1523231
Posted Wednesday, January 1, 2014 12:53 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Yesterday @ 8:14 PM
Points: 35,215, Visits: 31,665
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."

(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 #1526948
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse