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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:00:46+00:00 2026-06-18T18:00:46+00:00

I have a table that holds values for particular months: | MFG | DATE

  • 0

I have a table that holds values for particular months:

| MFG |       DATE | FACTOR |
-----------------------------
|   1 | 2013-01-01 |      1 |
|   2 | 2013-01-01 |    0.8 |
|   2 | 2013-02-01 |      1 |
|   2 | 2013-12-01 |   1.55 |
|   3 | 2013-01-01 |      1 |
|   3 | 2013-04-01 |    1.3 |
|   3 | 2013-05-01 |    1.2 |
|   3 | 2013-06-01 |    1.1 |
|   3 | 2013-07-01 |      1 |
|   4 | 2013-01-01 |    0.9 |
|   4 | 2013-02-01 |      1 |
|   4 | 2013-12-01 |    1.8 |
|   5 | 2013-01-01 |    1.4 |
|   5 | 2013-02-01 |      1 |
|   5 | 2013-10-01 |    1.3 |
|   5 | 2013-11-01 |    1.2 |
|   5 | 2013-12-01 |    1.5 |

What I would like to do is pivot these using a calendar table (already defined):

And finally, cascade the NULL columns to use the previous value.

What I’ve got so far is a query that will populate the NULLs with the last value for mfg = 3. Each mfg will always have a value for the first of the year. My question is; how do I pivot this and extend to all mfg?

SELECT c.[date], 
       f.[factor], 
       Isnull(f.[factor], (SELECT TOP 1 factor 
                           FROM   factors 
                           WHERE  [date] < c.[date] 
                                  AND [factor] IS NOT NULL 
                                  AND mfg = 3 
                           ORDER  BY [date] DESC)) AS xFactor 
FROM   (SELECT [date] 
        FROM   calendar 
        WHERE  Datepart(yy, [date]) = 2013 
               AND Datepart(d, [date]) = 1) c 
       LEFT JOIN (SELECT [date], 
                         [factor] 
                  FROM   factors 
                  WHERE  mfg = 3) f 
              ON f.[date] = c.[date] 

Result

|       DATE | FACTOR | XFACTOR |
---------------------------------
| 2013-01-01 |      1 |       1 |
| 2013-02-01 | (null) |       1 |
| 2013-03-01 | (null) |       1 |
| 2013-04-01 |    1.3 |     1.3 |
| 2013-05-01 |    1.2 |     1.2 |
| 2013-06-01 |    1.1 |     1.1 |
| 2013-07-01 |      1 |       1 |
| 2013-08-01 | (null) |       1 |
| 2013-09-01 | (null) |       1 |
| 2013-10-01 | (null) |       1 |
| 2013-11-01 | (null) |       1 |
| 2013-12-01 | (null) |       1 |

SQL Fiddle

  • 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-06-18T18:00:47+00:00Added an answer on June 18, 2026 at 6:00 pm

    Don’t know if you need the dates to be dynamic from the calender table or if mfg can be more than 5 but this should give you some ideas.

    select *
    from (
          select c.date,
                 t.mfg,
                 (
                 select top 1 f.factor 
                 from factors as f
                 where f.date <= c.date and
                       f.mfg = t.mfg and
                       f.factor is not null
                 order by f.date desc
                 ) as factor      
          from calendar as c
            cross apply(values(1),(2),(3),(4),(5)) as t(mfg)
         ) as t
    pivot (
          max(t.factor) for t.date in ([20130101], [20130201], [20130301], 
                                       [20130401], [20130501], [20130601], 
                                       [20130701], [20130801], [20130901], 
                                       [20131001], [20131101], [20131201])
          ) as P
    

    SQL Fiddle

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

Sidebar

Related Questions

I have a configuration table, that holds the values selected by a specific user.
i have a table that holds my users information like username , password ,
I basically have a table that holds counts for every date. I want to
I have a table in my database that holds numerical values collected from a
I have a field in an Azure table that holds date times in string
I have a table that holds information about a particular Object, Say Item and
The StringMapBase SQL table is the table that holds Option List values that have
Given that I have a table that holds vehicle information and one of those
I have a MySQL table that holds many entries with repeated IDs (for various
Hi I have a datatables based table that holds data. I need to be

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.