Mulling this over in my head before starting the code and I am stuck on part of it.
I have two dates in my table and an ISO date for today.
- 2010-12-11
- 2010-12-17
- 2010-12-09
I need to work out how many of the date range are within several time slots eg less than 1 day, less than three days and less then 7.
(the day numbers are not set in concrete so I need to make the same query work if those numbers differ).
I am thinking along the lines of this code, for when checking just one date but I need to build it into one query for all the ‘day numbers’ I may use
select count(*)
from table1 AS t1
where (
( date_diff ( 2010-12-09 , t1.check_in_date) < 3 )
OR
( date_diff ( 2010-12-09 , t1.check_in_date + 1 ) < 3 )
OR
( date_diff ( 2010-12-09 , t1.check_in_date + 2 ) < 3 )
)
select count(*)
from table2 AS t2
where (
(
( date_diff ( 2010-12-09 , t2.check_in_date ) <= 5 )
AND
( date_diff ( 2010-12-09 , t2.check_in_date ) > 3 )
)
OR
(
( date_diff ( 2010-12-09 , t2.check_in_date + 1 ) <= 5 )
AND
( date_diff ( 2010-12-09 , t2.check_in_date +1 ) > 3 )
)
OR
(
( date_diff ( 2010-12-09 , t2.check_in_date + 2 ) <= 5 )
AND
( date_diff ( 2010-12-09 , t2.check_in_date + 2 ) > 3 )
)
OR
(
( date_diff ( 2010-12-09 , t2.check_in_date + 3 ) <= 5 )
AND
( date_diff ( 2010-12-09 , t2.check_in_date + 3 ) > 3 )
)
OR
(
( date_diff ( 2010-12-09 , t2.check_in_date + 4 ) <= 5 )
AND
( date_diff ( 2010-12-09 , t2.check_in_date + 4 ) > 3 )
)
)
select count(*)
from table3 AS t3
where (
(
( date_diff ( 2010-12-09 , t3.check_in_date ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date ) > 5 )
)
OR
(
( date_diff ( 2010-12-09 , t3.check_in_date + 1 ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date +1 ) > 5 )
)
OR
(
( date_diff ( 2010-12-09 , t3.check_in_date + 2 ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date + 2 ) > 5 )
)
OR
(
( date_diff ( 2010-12-09 , t3.check_in_date + 3 ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date + 3 ) > 5 )
)
OR
(
( date_diff ( 2010-12-09 , t3.check_in_date + 4 ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date + 4 ) > 5 )
)
OR
(
( date_diff ( 2010-12-09 , t3.check_in_date + 5 ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date + 5 ) > 5 )
)
OR
(
( date_diff ( 2010-12-09 , t3.check_in_date + 6 ) <= 7 )
AND
( date_diff ( 2010-12-09 , t3.check_in_date + 6 ) > 5 )
)
)
It looks like a UNION ALL might do it but all that code looks too repetitive to be correct.
I need to bring back, how many days are less than 7 from ‘today’, how many are less than 5 days from ‘today’ and how many are less than 3 days away from today. BUT, I need to make sure that if a date is within 3 days, that it isn’t also counted as being within 5 days.
bazz