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

  • Home
  • SEARCH
  • 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 6021727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:45:20+00:00 2026-05-23T03:45:20+00:00

I have a table of items by date (each row is a new date).

  • 0

I have a table of items by date (each row is a new date). I am drawing out a value from another column D. I need it to replace 0s though. I need the following logic: when D=0 for that date, use the value in column D from the date prior.

Actually, truth be told, I need it to say, when D is 0, use the value from the latest date where D was not a 0, but the first will get me most of the way there.

Is there a way to build this logic? Maybe a CTE?

Thank you very much.

PS I’m using SSMS 2008.

EDIT: I wasn’t very clear at first. The value I want to change is not the date. I want change the value in D with the latest non-zero value from D, based on date.

  • 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-23T03:45:21+00:00Added an answer on May 23, 2026 at 3:45 am

    May be the following query might help you. It uses the OUTER APPLY to fetch the results. Screenshot #1 shows the sample data and query output against the sample data. This query can be written better but this is what I could come up with right now.

    Hope that helps.

    SELECT      ITM.Id
            ,   COALESCE(DAT.New_D, ITM.D) AS D
            ,   ITM.DateValue
    FROM        dbo.Items   ITM
    OUTER APPLY (
                    SELECT      
                    TOP 1       D   AS New_D
                    FROM        dbo.Items DAT
                    WHERE       DAT.DateValue   < ITM.DateValue
                    AND         DAT.D           <> 0
                    AND         ITM.D           = 0
                    ORDER BY    DAT.DateValue DESC
                ) DAT
    

    Screenshot #1:

    1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of items, each of which has a date associated with
I have a table of items, and a table of itemkeywords. When a user
I have a table with say 1640 items. I set bindingSource.Filter = some filter
I have a list of items that is refreshed daily where each item falls
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have these 3 tables + data: items : itemId, itemName data: 1, my
I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
Simplified for example, I have two tables, groups and items . items ( id,
I have a History Table in SQL Server that basically tracks an item through
I have table inside a div tab. The table has 40 rows in it

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.