> If you need some examples of problem solving using database
> technology, here are a couple examples I developed in mysql, but could
> be done with any recent database technology:
>
> Hexagonal puzzle solution:
> http://www.softfrontiers.com/Database_Solutions/HexPuzzle.shtml
It strikes me that the sequences could be generated from the pieces
using a stored procedure. (this would save lots of typing)
Also the answer could be got by doing a select on the piece table
with joins the sequence table 6 times with constraints that make the
numbers line up and a where clause that makes each piece unique.
you'll then get a row for each solution.
> Date range set solution:
> http://www.softfrontiers.com/Database_Solutions/DateRangeProblem.shtml
temp tables, stored procedures...
Is everything so hard to do in mysql?
postgresql has date arithmetic, and intervals with a unit month
which behaves as expected so, (other inverval units like second and
year, but month is the interesting one)
in postgresql, this:
SELECT
extract(year from m) as "Year",
extract(Month from m) as "ID",
to_char(m,'MON') as "Month",
m as "MonthStart",
m - 6 - (extract( dow from m) + 1)::integer % 7 as "start",
(m + '1 month'::interval ):: date - 7 -
(extract( dow from (m + '1 month'::interval)) + 1)::integer % 7 as "end"
FROM
(select ('2008-07-01'::date + generate_series(0,23) * '1 month'::interval) ::date as m) as base
;
produces this:
Year | ID | Month | MonthStart | start | end
------+----+-------+------------+------------+------------
2008 | 7 | JUL | 2008-07-01 | 2008-06-22 | 2008-07-19
2008 | 8 | AUG | 2008-08-01 | 2008-07-20 | 2008-08-23
2008 | 9 | SEP | 2008-09-01 | 2008-08-24 | 2008-09-20
2008 | 10 | OCT | 2008-10-01 | 2008-09-21 | 2008-10-25
2008 | 11 | NOV | 2008-11-01 | 2008-10-26 | 2008-11-22
2008 | 12 | DEC | 2008-12-01 | 2008-11-23 | 2008-12-20
2009 | 1 | JAN | 2009-01-01 | 2008-12-21 | 2009-01-24
2009 | 2 | FEB | 2009-02-01 | 2009-01-25 | 2009-02-21
2009 | 3 | MAR | 2009-03-01 | 2009-02-22 | 2009-03-21
2009 | 4 | APR | 2009-04-01 | 2009-03-22 | 2009-04-18
2009 | 5 | MAY | 2009-05-01 | 2009-04-19 | 2009-05-23
2009 | 6 | JUN | 2009-06-01 | 2009-05-24 | 2009-06-20
2009 | 7 | JUL | 2009-07-01 | 2009-06-21 | 2009-07-25
2009 | 8 | AUG | 2009-08-01 | 2009-07-26 | 2009-08-22
2009 | 9 | SEP | 2009-09-01 | 2009-08-23 | 2009-09-19
2009 | 10 | OCT | 2009-10-01 | 2009-09-20 | 2009-10-24
2009 | 11 | NOV | 2009-11-01 | 2009-10-25 | 2009-11-21
2009 | 12 | DEC | 2009-12-01 | 2009-11-22 | 2009-12-19
2010 | 1 | JAN | 2010-01-01 | 2009-12-20 | 2010-01-23
2010 | 2 | FEB | 2010-02-01 | 2010-01-24 | 2010-02-20
2010 | 3 | MAR | 2010-03-01 | 2010-02-21 | 2010-03-20
2010 | 4 | APR | 2010-04-01 | 2010-03-21 | 2010-04-24
2010 | 5 | MAY | 2010-05-01 | 2010-04-25 | 2010-05-22
2010 | 6 | JUN | 2010-06-01 | 2010-05-23 | 2010-06-19
(24 rows)
how it works.
in the calculation of "start" above
extract( dow from m)
is the day of the week (0=sunday)
(extract( dow from m) + 1)::integer % 7
is then number of days since since saturday
(0 if the first of the month is saturday)
so subtracting this many days from today will give the saturday
after the last friday in the previous month
and subtracting 6 more days will give the sunday before the last
friday of the previous month.
Bye.
Jasen
DTecMeister - 23 Sep 2008 15:16 GMT
> > If you need some examples of problem solving using database
> > technology, here are a couple examples I developed in mysql, but could
[quoted text clipped - 87 lines]
> Bye.
> Jasen
Jasen,
Thanks for the analysis.
Yes, I could have used a subquery in the date solution. I didn't use
the stored procedure to fill the sequences because the version of
mysql I was using for that problem didn't support them. That would be
a good way to tackle the problem if it was available.
As for solving the puzzle with your suggestion, maybe it's possible.
I didn't try it. I'm happy with the solution shown.
Jeff