Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:54:23+00:00 2026-05-28T04:54:23+00:00

I have this simple problem in SQL. I have a table with 4 columns.

  • 0

I have this simple problem in SQL. I have a table with 4 columns. Columns being ID, DateTime, Decimal, AnotherID.

DECLARE @data DATETIME 
SET @data = '20120101'

SELECT [ID]
      ,[Data]
      ,[Value]
      ,[InID]   
 FROM [dbo].[Static] 
 WHERE InID = 1 AND [Data] <= @data

Now I have to choose proper Value based on InID and Data. InID can have multiple rows with different dates. This is example output for InID = 1.

ID          Data            Value              InID
389         2012-01-02      10.00000000     1
390         2011-12-16      20.00000000     1
391         2011-12-13      15.00000000     1

Now based on InID and Data I need to get Value for given date. For example for InID = 1 I need to get Value for day 2011-12-14. Of course the value should be taken from 13th of December and if I ask it about value for date 2012-01-13 it should take last value. However in case the question is about date before the first date it should return NULL. I tried multiple combinations but my brain seems to be out of order atm so any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:54:24+00:00Added an answer on May 28, 2026 at 4:54 am

    Try this to get only the most recent Value constrained by the @data parameter:

    SELECT TOP 1 [Value]
    FROM [dbo].[Static] 
    WHERE InID = 1 AND [Data] <= @data
    ORDER BY [Data] DESC;
    

    To the last part of your question if you need a NULL result you’ll have to wrap the query above as it will return an empty result set if nothing qualifies:

    DECLARE @theValue float;
    SELECT @theValue = (
      SELECT TOP 1 [Value]
      FROM [dbo].[Static] 
      WHERE InID = 1 AND [Data] <= @data
      ORDER BY [Data] DESC
    );
    

    The nearest would be this (just be aware that it can grab either future or past dates):

     SELECT TOP 1 [Value]
     FROM [dbo].[Static]
     WHERE InID = 1
     ORDER BY ABS(DATEDIFF(Day, [Data], @data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm guessing this is a simple problem, but I'm just learning... I have this:
This seems like a simple problem: I have a WF4 activity that guides the
I've wrote this simple piece of code. And I have a slight problem with
I have a simple table made up of two columns: col_A and col_B .
(Note: this is for MS SQL Server) Say you have a table ABC with
I have a mysql table , which holds data like this. Name |how_out |runs
[Using SQL Server 2000] I can't believe a simple task is being this complex
I have this problem where I can't get my dynamic data web app to
I have this simple regex, [\d]{1,5} that matches any integer between 0 and 99999.
I have this simple Jsp page: <%@ page language=java import=java.awt.Color%> <% Color background =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.