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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:40:45+00:00 2026-06-17T12:40:45+00:00

I have a table that I want to join with a calendar table to

  • 0

I have a table that I want to join with a calendar table to produce a time series and I’m doing it this way:

SELECT thedate AS Date
INTO #CALENDAR
FROM
(
    SELECT *
    FROM Sample.dbo.ExplodeDates('20120101','20121231') as d
) X
GO

CREATE INDEX D ON #CALENDAR(Date)
GO

    SELECT Date, A, B, Value
FROM
(
      SELECT StartDate, A, B, Value
      FROM Sample.dbo.Values
) X
RIGHT OUTER JOIN #CALENDAR
    ON Date = StartDate
ORDER BY Date

DROP TABLE #CALENDAR
GO

This works except that the columns of A and B contain NULLs i.e. I’m getting something like this:

2012-04-06 00:00:00.000 NULL    NULL    NULL
2012-04-07 00:00:00.000 NULL    NULL    NULL
2012-04-08 00:00:00.000 NULL    NULL    NULL
2012-04-09 00:00:00.000 A1  B1  4256
2012-04-10 00:00:00.000 A1  B1  4498

Is there a way I can get something like this:

2012-04-06 00:00:00.000 A1  B1  0
2012-04-07 00:00:00.000 A1  B1  0
2012-04-08 00:00:00.000 A1  B1  0
2012-04-09 00:00:00.000 A1  B1  4256
2012-04-10 00:00:00.000 A1  B1  4498

I gave this example for one pair (A1,B1) but there are others. Any suggestions on how to achieve this?

Update:

Better stand-alone example:

-- The explore dates function

USE Sample
GO

/****** Object:  UserDefinedFunction [dbo].[ExplodeDates]    Script Date: 01/18/2013 01:08:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ExplodeDates]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'CREATE FUNCTION [dbo].[ExplodeDates](@startdate datetime, @enddate datetime)
returns table as
return (
with 
 N0 as (SELECT 1 as n UNION ALL SELECT 1)
,N1 as (SELECT 1 as n FROM N0 t1, N0 t2)
,N2 as (SELECT 1 as n FROM N1 t1, N1 t2)
,N3 as (SELECT 1 as n FROM N2 t1, N2 t2)
,N4 as (SELECT 1 as n FROM N3 t1, N3 t2)
,N5 as (SELECT 1 as n FROM N4 t1, N4 t2)
,N6 as (SELECT 1 as n FROM N5 t1, N5 t2)
,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6)
SELECT DATEADD(day,num-1,@startdate) as thedate
FROM nums
WHERE num <= DATEDIFF(day,@startdate,@enddate) + 1
);' 
END

GO

SELECT thedate AS Date
INTO #CALENDAR
FROM
(
    SELECT *
    FROM Sample.dbo.ExplodeDates('20120101','20120103') as d
) X
GO

CREATE INDEX D ON #CALENDAR(Date)
GO

CREATE TABLE #TEMP(StartDate datetime, A varchar(50), B varchar(50), Value int)

INSERT INTO #TEMP VALUES('2012-01-01', 'A1', 'A1B1', 10)
INSERT INTO #TEMP VALUES('2012-01-02', 'A1', 'A1B2', 10)
INSERT INTO #TEMP VALUES('2012-01-02', 'A2', 'A2B2', 100)


-- SELECT all traffic values
SELECT Date, A, B, Value
FROM
(
  SELECT StartDate, A, B, Value
  FROM #TEMP
) X
RIGHT OUTER JOIN #CALENDAR
ON Date = StartDate
ORDER BY A, B, Date

DROP TABLE #CALENDAR
DROP TABLE #TEMP
GO

The output it gives me now:

2012-01-03 00:00:00.000 NULL    NULL    NULL
2012-01-01 00:00:00.000 A1  A1B1    10
2012-01-02 00:00:00.000 A1  A1B2    14
2012-01-02 00:00:00.000 A2  A2B2    100

The output I want:

2012-01-01 00:00:00.000 A1  A1B1    0
2012-01-02 00:00:00.000 A1  A1B1    10
2012-01-03 00:00:00.000 A1  A1B1    0
2012-01-01 00:00:00.000 A1  A1B1    10
2012-01-02 00:00:00.000 A1  A1B2    14
2012-01-03 00:00:00.000 A1  A1B2    0
2012-01-01 00:00:00.000 A2  A2B2    0
2012-01-02 00:00:00.000 A2  A2B2    100
2012-01-03 00:00:00.000 A2  A2B2    0
  • 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-17T12:40:46+00:00Added an answer on June 17, 2026 at 12:40 pm
    select 
        b.date, 
        a.A, 
        a.B, 
        case 
          when a.date = b.date then a.value 
          else 0 
        end as Value
    from #temp a 
    cross join #calendar b
    -- where b.date between -- as calendar filter
    

    isnt it example of the typical cross join
    i havent tested but will do in as sec 😀

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

Sidebar

Related Questions

i have a table that has event name and event date. if i want
I have one table vwuser. I want join this table with the table valued
I basically have a table that holds counts for every date. I want to
I have a table that looks like this: pk client value date 1 001
All, I have a table that looks like this: Date Pitcher WHIP -------- --------------
I have a join table that I want to index only for the table
let's say that you want to select all rows from one table that have
I have to select requests that i want to combine using UNION : Table
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have 4 different tables that I want to join. The tables are structured

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.