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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:10:06+00:00 2026-05-31T00:10:06+00:00

This is a bit tricky to explain, but I will do my best. Say

  • 0

This is a bit tricky to explain, but I will do my best.

Say I have two tables:

Dates (DateColumn datetime)

SomeData (ID1 int, ID2 int, SomeDate datetime)


Dates ALWAYS contains the following dates:

1/1/2012
1/2/2012
1/3/2012

SomeData contains data similar to this:

ID  ID1 ID2 SomeDate
1   1   4   1/1/2012
2   1   4   1/2/2012
3   2   3   1/1/2012
4   2   3   1/3/2012
6   5   1   1/2/2012

Now, notice how in SomeData, some of the rows have the same ID1 and ID2 combinations. Also notice how some of them are missing certain dates found in the Dates table. I need to somehow join each of those combinations with EVERY date in the Dates table, to find the missing dates.

So on joining, the data would end up looking like this:

ID      ID1 ID2 SomeDate
1       1   4   1/1/2012
2       1   4   1/2/2012
NULL    1   4   1/3/2012 <-- missing from SomeData table

3       2   3   1/1/2012
NULL    2   3   1/2/2012 <-- missing from SomeData table
4       2   3   1/3/2012

NULL    5   1   1/1/2012 <-- missing from SomeData table
6       5   1   1/2/2012
NULL    5   1   1/3/2012 <-- missing from SomeData table

Notice how each ID1+ID2 combination each returns 3 records, using all the dates in the Dates table, and also notice how each “missing” record maintains the ID1+ID2 combination. Any idea how I can pull this off?

Thanks in advance!

  • 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-31T00:10:07+00:00Added an answer on May 31, 2026 at 12:10 am

    CROSS JOIN is your friend here. Here’s a full worked example

    DECLARE @Dates TABLE (DateColumn DATETIME)
    INSERT @Dates VALUES ('20120101'), ('20120102'), ('20120103')
    
    DECLARE @SomeData TABLE (ID INTEGER IDENTITY(1,1), ID1 INTEGER, ID2 INTEGER, 
        SomeDate DATETIME)
    INSERT @SomeData VALUES (1,4,'20120101'),(1,4,'20120102'),(2,3,'20120101'),
        (2,3,'20120103'), (5,1,'20120102')
    
    
    SELECT sd2.ID, sd1.ID1, sd1.ID2, d.DateColumn
    FROM
    (
        SELECT ID1, ID2
        FROM @SomeData
        GROUP BY ID1, ID2
    ) sd1
        CROSS JOIN @Dates d
        LEFT JOIN @SomeData sd2 ON sd1.ID1 = sd2.ID1 
                AND sd1.ID2 = sd2.ID2 
                AND sd2.SomeDate = d.DateColumn
    

    So to break it down:

    1. “sd1” subquery is getting a distinct set of all ID1 and ID2 combinations
    2. This is then CROSS APPLY’d to the Dates table. The result of this, is for each ID1+ID2 combination, it will result in a row being returned for each date in the Dates table
    3. the final LEFT JOIN then tries to match on the combination of ID1+ID2+Date to return the ID value. This returns NULL as the ID if no match is found, or the ID if it is found.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a bit tricky to explain but I will try my best to
This might be a bit tricky setup but... I have this Silverlight project that
This one is a bit tricky to explain. I have a usercontrol with some
Bit tricky to communicate but hope this makes sense. I have 8 div containers
This is a bit tricky to explain, i'm making an API where i take
It's a bit tricky to describe css issues, but I'll do my best. Below
This is a bit tricky question for you computer scientists. Let's say that I
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have this bit of script to widen a text box on mouseover and
I have this bit of code I found on the web at the end

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.