1

I have a discrete event in time that I am trying to join against a continuous calendar.

As an example, let's say I have a calendar of dates that looks like the following:

Dates
1-Jan-2014
2-Jan-2014
3-Jan-2014 ...
31-Dec-2014

Now imagine that you have a discrete event in time, say a sale, that you want to flag in your calendar. In this example, the sale is from 3-Mar to 7-Mar.

The output would therefore look like:

Dates, Sales_flag
...
2-Mar-2014, FALSE
3-Mar-2014, TRUE
4-Mar-2014, TRUE
...
7-Mar-2014, TRUE
8-Mar-2014, FALSE

... or even be able to just return the rows that are in the sale would be a start, i.e.:

Date
3-Mar-2014
4-Mar-2014
...
7-Mar-2014

So far, I think I need to use a right outer join in SQL to achieve this. However, I'm not sure how to join over a range of values. Here is what I have so far:

(NB Using Access due to corporate policy on what databases we can use.)

SELECT Sales_Flag
FROM Sales
RIGHT JOIN Calendar
ON Sales.Date = Calendar.[Start Date]
WHERE Calendar.Date BETWEEN Sales.[Start Date] and Sales.[End Date];

However, all of my attempts to date have resulted in me having one row with just the Start Date, which I completely understand -- ultimately I'm joining on a single value, hence get a single row as a result.

Therefore, am I even heading in the right direction, or barking in completely the wrong shrub?

1 Answer 1

1

Right idea...but I don't get why you are joining the table on only the start date. Why not join on the range of dates?

SELECT Sales_Flag
FROM Sales
RIGHT JOIN Calendar
ON Calendar.Date BETWEEN Sales.[Start Date] and Sales.[End Date];

I'm kinda curious why the term 'discrete' event here...was the sale behind closed doors? :)

where sales.sales_flag is not null

That where clause will eliminate all rows that are not matched from the calendar.

3
  • Aha -- never knew that you could join on a range... that has certainly moved the prawn somewhat. Is this known as a specific Join technique? -- I've only joined on single values before. Further, Access gives me a annoying pointless error message of "Between operator without And in query expression 'Calendar.Date BETWEEN Sales.[Start Date'". Any thoughts on what I've got wrong? Feb 12, 2014 at 13:22
  • Unfortunately no, Access sql is it's own language and doesn't make the most sense to me. Try googling access between syntax and you should get a hit there. Worst case...don't use between, do a date >start and date < end.
    – Twelfth
    Feb 12, 2014 at 17:49
  • Sorry, missed one of your questions...not sure if it's known by any specific name. Back in the old days, join clauses didn't exist and everything would be put into the where clause (including the joins)...anything you can include in a where statement could technically be included in a join clause. Ranges, case statements, custom functions...you can really join on anything (or any combination of) that you want.
    – Twelfth
    Feb 13, 2014 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.