Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

query help, getting the row that fits the date

$
0
0

Here is a sample dataset

CREATE TABLE #TESTDATA (
TableID INT IDENTITY(1,1),
SomeCode VARCHAR(50),
SomeDescription VARCHAR(50),
EffectiveFrom DATE
)

INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('A','blah1','2014-01-03')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('A','blah2','2014-01-10')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('A','blah3','2014-01-15')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('A','blah4','2014-01-30')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('B','blahblah1','2014-01-01')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('B','blahblah2','2014-01-05')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('B','blahblah3','2014-01-20')
INSERT #TESTDATA (SomeCode,SomeDescription,EffectiveFrom) VALUES ('B','blahblah4','2014-01-30')

The dates in the EffectiveFrom column indicates the date from which SomeDescription applies to SomeCode. So when someone asks what was the SomeDescription for SomeCode = A on 2014-01-04, the answer would be blah1.

On '2014-01-10', it would be blah2.

Anything on or past '2014-01-30' would be blah4.

Nothing if before '2014-01-03'

So how would I do a query that says give me every SomeCode and SomeDescription pair that was effective on @Date. I purposely left out the EffectiveTo date which would make this query very easy by doing a between clause.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>