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 6252297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:46:15+00:00 2026-05-24T13:46:15+00:00

for SQL Server 2008 R2 I have a resultset that looks like this (note

  • 0

for SQL Server 2008 R2

I have a resultset that looks like this (note [price] is numeric, NULL below represents a
NULL value, the result set is ordered by product_id and timestamp)

product timestamp          price 
------- ----------------   -----
   5678 2008-01-01 12:00   12.34
   5678 2008-01-01 12:01    NULL
   5678 2008-01-01 12:02    NULL
   5678 2008-01-01 12:03   23.45
   5678 2008-01-01 12:04    NULL

I want to transform that to a result set that (essentially) copies a non-null value from the latest preceding row, to produce a resultset that looks like this:

product timestamp          price  
------- ----------------   -----
   5678 2008-01-01 12:00   12.34
   5678 2008-01-01 12:01   12.34
   5678 2008-01-01 12:02   12.34
   5678 2008-01-01 12:03   23.45
   5678 2008-01-01 12:04   23.45

I don’t find any aggregate/windowing function that will allow me to do this (again this ONLY needed for SQL Server 2008 R2.)

I was hoping to find an analytic aggregate function that do this for me, something like…

LAST_VALUE(price) OVER (PARTITION BY product_id ORDER BY timestamp)

But I don’t seem to find any way to do a “cumulative latest non-null value” in the window (to bound the window to the preceding rows, rather than the entire partition)

Aside from creating a table-valued user defined function, is there any builtin that would accomplish this?


UPDATE:

Apparently, this functionality is available in the ‘Denali’ CTP, but not in SQL Server 2008 R2.

LAST_VALUE http://msdn.microsoft.com/en-us/library/hh231517%28v=SQL.110%29.aspx

I just expected it to be available in SQL Server 2008. It’s available in Oracle (since 10gR2 at least), and I can do something similar in MySQL 5.1, using a local variable.

http://download.oracle.com/docs/cd/E14072_01/server.112/e10592/functions083.htm

  • 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-24T13:46:15+00:00Added an answer on May 24, 2026 at 1:46 pm

    You can try the following:

    * Updated **

    -- Test Data
    DECLARE @YourTable TABLE(Product INT, Timestamp DATETIME, Price NUMERIC(16,4))
    
    INSERT INTO @YourTable
    SELECT 5678, '20080101 12:00:00', 12.34
    UNION ALL
    SELECT 5678, '20080101 12:01:00', NULL
    UNION ALL
    SELECT 5678, '20080101 12:02:00', NULL
    UNION ALL
    SELECT 5678, '20080101 12:03:00', 23.45
    UNION ALL
    SELECT 5678, '20080101 12:04:00', NULL
    
    ;WITH CTE AS
    (
        SELECT *
        FROM @YourTable
    )
    
    -- Query
    SELECT A.Product, A.Timestamp, ISNULL(A.Price,B.Price) Price
    FROM CTE A
    OUTER APPLY (   SELECT TOP 1 *
                    FROM CTE 
                    WHERE Product = A.Product AND Timestamp < A.Timestamp
                    AND Price IS NOT NULL
                    ORDER BY Product, Timestamp DESC) B
    
    --Results
    Product Timestamp   Price
    5678    2008-01-01 12:00:00.000 12.3400
    5678    2008-01-01 12:01:00.000 12.3400
    5678    2008-01-01 12:02:00.000 12.3400
    5678    2008-01-01 12:03:00.000 23.4500
    5678    2008-01-01 12:04:00.000 23.4500
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL Server (2005/2008) Each of the below statements have the same result. Does anyone
I have a couple of tables in a SQL 2008 server that I need
I have 3 home PCs that have 2008 SQL Server Express instances installed on
I have an NHibernate query that looks like this: var query = Session.CreateQuery(@ select
I have a SQL Server 2008 procedure that sends email via sp_send_dbmail. I'm using
I have tried SQL Server 2008 Management Studio and other third party tools to
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just
I have looked at the SQL Server 2008 feature comparison matrix and it lists
I have two named instances of SQL Server 2008 and am trying to set
anyone have script or procedures to install SQL Server 2008 Express, set up the

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.