I'm building this report in a system for a Billboard company.
They have a table that stores all their billboards, and among other data, billboards have a Start Date and a Finish Date (both can be null).
If for some reason a billboard has to stop beeing used, they set a finish date and it will become unnavaiable to be used after that date. Same with Start date (in case they just set up a new board and it will be avaiable for use after a set Start Date).
In this report I have to get the sum of all possible advertising spots in a given period of time.
So, lets say that for the period I chose (4 weeks, for exemple)
* In week 1 there are 500 avaiable boards
* In week 2 one board became unavaiable (making 498 boards avaiable)
* In week 3 two boards became avaiable (making 501 boards avaiable)
* In week 4 one board became avaiable and one other became unavaiable (making 501 boards avaiable)
So then I should have a total of 1990 avaiable boards in this period, that's the result I'm after.
How can I get this in a single query?
The weeks will come from a HTML form and I have to convert them to dates before the query, so I'll just have the dates I want to know how many boards are avaiable at.
The query to get the amount of boards for ONE specific date is like this:
SELECT
COUNT(IdBillboard)
FROM
tBillboards
WHERE
(StartDate IS NULL OR StartDate <= '2009-01-05 00:00:00')
AND (FinishDate IS NULL OR FinishDate >= '2009-01-05 00:00:00')
I can't just add AND
and OR
conditionals for each date because I kinda need a new recount for the separate dates. I thought about using a WHILE
but couldn't figure out exactly how to do it for this case. So that's where I'm stuck...
I'll post more details if anyone needs them.
Thanks, Gabe