Add option to contraint for overnight.

Some bugs in SQL are found and solved.
Total time
Show SQL