Query help, getting the row that fits the date

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.

Are you looking for the below?
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')
Declare @somecode varchar(10) = 'A', @sDate date = '2014-01-14'
Select Top 1 * From #TESTDATA where SomeCode = @somecode and EffectiveFrom <=@sDate Order by EffectiveFrom desc
Drop table #Testdata

Similar Messages

  • What are the cases that fit the late 2013 retina display MacBooks?

    So I recently bought a late 2013 MacBook Pro Retina Display and I know that they are a little thinner than previous models.  I want to know what cases would fit this laptop. My google searching abilities are very low.  If anyone can help me please let me know.

    It is the width that is more important.
    You do not say 13'' or 15''

  • I am trying to uninstall itunes and keep getting a reject that reads "the feature you are trying to use is on a network resource that is unavailable."  Its windows...help!

    I am trying to uninstall itunes and keep getting an error that reads "the feature you are trying to use is on a network resource that is unavailable."  Itunes on windows.  Help!

    (1) Download the Windows Installer CleanUp utility installer file (msicuu2.exe) from the following Major Geeks page (use one of the links under the "DOWNLOAD LOCATIONS" thingy on the Major Geeks page):
    http://majorgeeks.com/download.php?det=4459
    (2) Doubleclick the msicuu2.exe file and follow the prompts to install the Windows Installer CleanUp utility. (If you're on a Windows Vista or Windows 7 system and you get a Code 800A0046 error message when doubleclicking the msicuu2.exe file, try instead right-clicking on the msicuu2.exe file and selecting "Run as administrator".)
    (3) In your Start menu click All Programs and then click Windows Install Clean Up. The Windows Installer CleanUp utility window appears, listing software that is currently installed on your computer.
    (4) In the list of programs that appears in CleanUp, select any iTunes entries and click "Remove", as per the following screenshot:
    (5) Quit out of CleanUp, restart the PC and try another iTunes install. Does it go through properly this time?

  • After installing Mountain Lion, iMessages and Facetime does not work. When I try to sign-in I get a message that says: The server encountered an error processing registration. Please try again later. Apple care does not know what is the cause. Please help

    After installing Mountain Lion, iMessages and Facetime does not work. When I try to sign-in I get a message that says: The server encountered an error processing registration. Please try again later. After 4 calls to apple and 8 and a half hours on the phone. The apple people does not how to solve the problem. The last thing they told me is that they will send the problem to their engineers and I will hear from them. unfortunately they have not contact me.
    During the phone calls I tried putting the date and time in automatic, changing the username and password, I even tried using somebody elses username and password. Please help, facetime is my tool to telecomute, and it is hurting my job.

    I had the same problem and found the solution here:
    https://discussions.apple.com/thread/3189272

  • Please help. When exporting a 720p video using Quicktime Conversion in FCP it saves some extra temporary files to my computer. The file is called ICMMultiPassStorage. This file gets so large that eventually the program and exporting process crashes.

    Please help. When exporting a 720p video using Quicktime Conversion in FCP it saves some extra temporary files to my computer. The temporary file it saves is called ICMMultiPassStorage. This file gets so large that eventually the program and exporting process crashes. I have been able to locate the file using GrandPerspective and close out of FCP to delete it. But when I try exporting the movie again it creates the ICMMultiPassStorage file again. I have made sure my scratch disks are set to my external hard drive so I know that isn't the issue. How do I fix this problem so these temporary files are not being stored on my internal hard drive instead of my external?Thanks!

    Try exporting the timeline "using current settings" via Make QuickTime Movie, then submit that clip to Compressor for the transcode. This should be faster and less painful.

  • Help, I can no longer open my IPhoto, I get a message that says the library needs to be upgraded to work with this version of IPhoto

    Help, I can no longer open my I Photo's.  I get a message that says the library needs to be updated to work with this version of IPhoto.  What have I done and what can I do to view them again.

    You're welcome
    It should have showed you a progress bar, and hopefully it was moving steadily

  • I am having an with the dictation feature in OSX Mountain Lion.  I got Dictation enabled but when I got to use the feature all I get is the circles that cycle the purple color, shakes the the box disappears please help.

    I am having an with the dictation feature in OSX Mountain Lion.  I got Dictation enabled but when I got to use the feature all I get is the circles that cycle the purple color, shakes the the box disappears please help.

    See the discussions under "More Like This" at right.

  • Hi-I am trying to email photos from iPhoto and I keep getting a flag that says "the server does not recognize the username/password combination.  Can you help me to reset it?

    Hi…I am trying to email photos from iPhoto and I keep getting a flag that says "the server does not recognize the username/password combination.  Can you help me to reset it?

    Thank you very much for the quick reply and it was so easy.  Make my life much easier now
    Thanks so much

  • I need to get photos off a water damaged iPhone 3GS. Can I take out the component that has the photos (is this the main board?) and put it into another of the same phone to retrieve the photos? Please help

    I need to get photos off a water damaged iPhone 3GS. It spent time underwater, saltwater. I let it dry for a week, turned it on, the apple symbol came on, then it died. I need to retrieve my photos off it. Can I take out the component that has the photos (is this the main board?) and put it into another of the same phone to retrieve the photos? Please help

    You're talking about the logic board. The two most likely things to get toasted, with water damage, are the battery(it shorts out), & the logic board. The worst water damage would be salt water. I'm afraid your logic board is toast.

  • I have tried the following, "netsh winsock, reset", but I get a message that says the required operation requires elevation run as administrator . this is my laptop and I am an administrator, please help , itunes store is the only program i have issues

    I get a message that says, the required operation requires elevation <run as administrator>. I am the owner of this laptop and I have administrative authority, why does this show and how can I fix this. Itunes store is the ONLY program I am having trouble with.Very , very frustrating.!!!!!!!!!!!!

    I will assume you're using Windows Vista-7:
    Try these steps instead:
    1 Click start, and in the start menu search, type "cmd" (without the quotes) DO NOT PRESS ENTER!
    2. Right click the "cmd" search result
    3. Click "Run as Administrator"
    4. If you get a pop up message, click yes or continue
    5. Type the command in the window as you did above.
    In an effort to try to prevent programs from messing with your computer, programs don't run under your administrator account unless you tell them to or they ask. This gives the Administrator (elevated) privelleges that you need.

  • SQL query to get last 10 records in the table?

    Hi,
    Can anyone tell me the SQL query to get last 10 records in the table?
    Thanks!!
    MCP

    Please, define what "last" means. Sets are unordered by definition, so if you want to retrieve rows from a table in a specific order you need to specify what that order is - e.g. by maintaining a value in a column (or a combination of columns) that you can use in the ORDER BY clause of the SELECT statement.
    If, for instance, you kept the time when the row was inserted in a special column (InsertedTime), you could use this in your query like this:
    select top (10)
      <column list>
      from <table or view>
      where <restriction(s)>
      order by InsertedTime desc;
    ML
    Matija Lah, SQL Server MVP
    http://milambda.blogspot.com

  • Random selection of rows from a 2D array then subset both the rows that were selected and those that were not. Please see message below.

    For example, I have a 2D array with 46 rows and 400 columns. I would like to randomly select 46 data rows from the 2D array. By doing the random selection it means that not all individual 46 rows will be selected some rows may appear more than once as there may be some duplicates or triplicates in the random selection. The importan thing is that we will have randomly selected 46 rows of data (no matter that some rows appear more than once). Then I would like to subset these randomly selected 46 data rows (some which will be duplicated, or triplicated, etc.) and then also find and subset the rows that were not selected. Does this make sense? Then i would like to do this say 10 times for this data set. So that then I will have 2 by 10 data sets: the first 10 each with 46 rows and the other 10 with n rows depending on how many WERE NOT randomly selected. i hope that my explanation is clear. I am relatively new to Labview. It is really great so I am getting better! If anyone can help me with this problems it will be great. RVR

    Start by generating randon #s between 0 and 45. Run a for loop X times and in it use the random function, multiply the result by X and round down (-infinity). You can make this into a subVI, which you can reuse later. In the same loop, or in a different one, use Index Array to extract the rows which were selected (wiring the result out of the loop with auto indexing causes it to be rebuilt into a 2D array).
    One possible solution for the second part would be to go over the array of randomly generated numbers in a for loop and use Search 1D Array to find each of the numbers (i). If you get -1, it means the row wasn't selected and you can extract it.
    I hope this puts you on the right path. If not, don't be afraid to ask more.
    To learn more about LV, I suggest you read the LabVIEW user manual. Also, try searching this site and google for LabVIEW tutorials. Here and here are a couple you can start with. You can also contact your local NI office and join one of their courses.
    In addition, I suggest you read the LabVIEW style guide.
    Try to take over the world!

  • How do you get Itunes to go the free radio station list, I'm not talkiing about the one that lists the price next to the song that's playing, I'm talking about the one that lists about 50 diffrent genes.

    How do you get Itunes to go to the free radio station list, I'm not talking about the one that lists the price next to the song that's playing, I'm talking about the one that gives you 50 diffrent genes to select from?

    This forum is for questions from those managing sites on iTunes U, Apple's service for colleges and universities to post educational material in the iTunes Store. You'll be most likely to get help with this issue if you ask in the general iTunes forums.
    But answering quickly, if you mean the streaming stations, click the Internet item:
    If you don't see that item, checkmark "Internet Radio" in the General preferences in iTunes.
    Regards.

  • Trying to upload some photos to an e-mail an I get a message that says "The iPhoto Library is locked, on a locked disk, or you do not have permission to make changes to it."  To where do I navigate to unlock this so I can have access to my photo library?

    Trying to upload some photos to an e-mail an I get a message that says "The iPhoto Library is locked, on a locked disk, or you do not have permission to make changes to it."  To where do I navigate to unlock this so I can have access to my photo library?

    Couple of things to try:
    Restart your mac and try launch iPhoto again.
    If that fails:
    Go to your Pictures Folder and find the iPhoto Library there. Right (or Control-) Click on the icon and select 'Show Package Contents'. A finder window will open with the Library exposed.
    Look there for a file called ‘iPhoto’Lock.data or similar. Drag it to the Desktop. Make no other changes. Start iPhoto. Does that help?
    If not: Where is your iPhoto Library stored? If it's on an External Disk, what format is that?

  • Recently bought  a new computer and all my music appears in iTunes as well as on my computer in a saved folder. When I try to play the music in iTunes I get an error that reads"the song could not be used because the original file could not be found."

    Recently bought  a new computer and all my music appears in iTunes as well as on my computer in a saved folder. When I try to play the music in iTunes I get an error that reads"the song could not be used because the original file could not be found. Would you like to locate it?" Hope you can help me solve this issue.
    Thanks,
    Shellyboo

    This happens if the file is no longer where iTunes expects to find it. Possible causes are that you or some third party tool has moved, renamed or deleted the file, or that the drive it lives on has had a change of drive letter. It is also possible that iTunes has changed from expecting the files to be in the pre-iTunes 9 layout to post-iTunes 9 layout, or vice-versa, and so is looking in slightly the wrong place.
    Select a track with an exclamation mark, use Ctrl-I to get info, then cancel when asked to try to locate the track. Look on the summary tab for the location that iTunes thinks the file should be. Now take a look around your hard drive(s). Hopefully you can locate the track in question. If a section of your library has simply been moved, or a drive letter has changed, it should be possible to reverse the actions.
    Alternatively, as long as you can find a location holding the missing files, then you should be able to use my FindTracks script to reconnect them to iTunes .
    tt2

Maybe you are looking for

  • FCP still not working in 10.4.8

    So I am running 10.4.8 amd my FCP HD 4.5 will not open. All I get is a message that the application unexpectadly quit. I have tried to reinstall the program but it does not work. Any thoughts on what is going on. This all started when I installed 10.

  • Need an alternate method to create a pdf

    Hello, I am using: strRptFilter = "[Invoice Number]=" & invnum DoCmd.OutputTo acOutputReport, "InvoicePDF", acFormatPDF, ("c:\PDFInvoices\" & invnum & ".pdf"), False where strRptFilter is the On Open Event of the Report The problem is it takes about

  • BizTalk FTP send port issue

    Hi, I'm receiving following error while sending message to an ftp link from BizTalk 2010 in production. Unable to connect to FTP server "ipaddress" as user "username". Inner Exception details: "No FTP server found on the specified address. ". Previou

  • Live Interior 3D Standard printing issues

    I recently downloaded Live Interior 3D Standard for my MacBook Pro.  Cannot get the application to print.  Tried a couple of different printers that work on other applications and tried printing one of the provided templates.  Any help

  • OFT test dynamic Component

    Hi, i'am using OFT 8.40 to test a webapp with javascript build content. The documentation says "Oracle Functional Testing for Web Applications uses a heuristic program to determine and exclude machine-generated components of Web page source that can