|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello all,
I am working on Martin Minka's date diff function as found at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful thing. However, I am trying to alter it or identify a similar function that instead of giving me the number of days between two dates it returns the number of workday hours:minutes between two datetimes, or some other date differential (such as an exact number of days between two dates with remainder) I hate asking open ended questions, but can anyone give me any hints as to how to make this conversion? I may be able to figure it out... In a few weeks. My long term goal is to identify the business hours(minutes, seconds, whatever) between two dates, taking into account weekends, holidays, and business hours. Thanks, Craig -------------------------------------------------------- This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Does anybody know what is the problem. MySql ran out of memory and I
have not been able to start it again. Here's an error from log file: 070827 15:09:17 mysqld started ^G/usr/sbin/mysqld: Character set 'utf8 |' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index.xml' file 070827 15:09:17 [ERROR] Aborting 070827 15:09:17 [Note] /usr/sbin/mysqld: Shutdown complete 070827 15:09:17 mysqld ended Any appreciated. -----Original Message----- From: Weston, Craig (OFT) [mailto:Craig.Weston@oft.state.ny.us] Sent: Monday, August 27, 2007 1:02 PM To: mysql@lists.mysql.com Subject: [MYSQL]Time formatting for cycle time. Hello all, I am working on Martin Minka's date diff function as found at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful thing. However, I am trying to alter it or identify a similar function that instead of giving me the number of days between two dates it returns the number of workday hours:minutes between two datetimes, or some other date differential (such as an exact number of days between two dates with remainder) I hate asking open ended questions, but can anyone give me any hints as to how to make this conversion? I may be able to figure it out... In a few weeks. My long term goal is to identify the business hours(minutes, seconds, whatever) between two dates, taking into account weekends, holidays, and business hours. Thanks, Craig -------------------------------------------------------- This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Craig,
>I am working on Martin Minka's date diff function as found >at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful >thing. However, I am trying to alter it or identify a similar function >that instead of giving me the number of days between two dates it >returns the number of workday hours:minutes between two datetimes, or >some other date differential (such as an exact number of days between >two dates with remainder) Here's a logically equivalent datediff calc, mebbe slightly simpler: SET @d1 = '2007-1-1'; SET @d2 = '2007-3-31'; SET @dow1 = DAYOFWEEK(@d1); SET @dow2 = DAYOFWEEK(@d2); SET @days = DATEDIFF(@d2,@d1); SET @wknddays = 2 * FLOOR( @days / 7 ) + IF( @dow1 = 1 AND @dow2 > 1, 1, IF( @dow1 = 7 AND @dow2 = 1, 1, IF( @dow1 > 1 AND @dow1 > @dow2, 2, if( @dow1 < 7 AND @dow2 = 7, 1, 0 ) ) ) ); SELECT FLOOR(@days-@wkndDays) AS BizDays; To include time in the difference, you could adopt as a return convention a string format like 'N days hh:mm:ss', where N is the date difference calculated above, minus one if the time portion of d1 is later than than that of d2. Something like this: SET @d1 = '2007-1-1 00:00:00'; SET @d2 = '2007-3-31 12:00:00'; SET @tdiff = TIMEDIFF( TIME(@d1), TIME(@d2) ); SET @dow1 = DAYOFWEEK(@d1); SET @dow2 = DAYOFWEEK(@d2); SET @days = DATEDIFF(@d2,@d1); SET @wknddays = 2 * FLOOR( @days / 7 ) + IF( @dow1 = 1 AND @dow2 > 1, 1, IF( @dow1 = 7 AND @dow2 = 1, 1, IF( @dow1 > 1 AND @dow1 > @dow2, 2, IF( @dow1 < 7 AND @dow2 = 7, 1, 0 ) ) ) ); SET @days = FLOOR(@days - @wkndDays) - IF( @tdiff < 0, 1, 0 ); SET @tdiff = IF( ASCII(@tdiff) = 45, SUBSTRING(@tdiff,2), TIMEDIFF( '24:00:00', @tdiff )); SELECT CONCAT( @days, ' days ', @tdiff ); PB ----- Weston, Craig (OFT) wrote: > Hello all, > > I am working on Martin Minka's date diff function as found > at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful > thing. However, I am trying to alter it or identify a similar function > that instead of giving me the number of days between two dates it > returns the number of workday hours:minutes between two datetimes, or > some other date differential (such as an exact number of days between > two dates with remainder) > > > > > > I hate asking open ended questions, but can anyone give me any hints as > to how to make this conversion? I may be able to figure it out... In a > few weeks. > > > > My long term goal is to identify the business hours(minutes, seconds, > whatever) between two dates, taking into account weekends, holidays, and > business hours. > > > > Thanks, > > Craig > -------------------------------------------------------- > This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. > > > ------------------------------------------------------------------------ > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.5.484 / Virus Database: 269.12.9/975 - Release Date: 8/26/2007 9:34 PM > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Wow, thanks. Lots to think about.
________________________________ From: Peter Brawley [mailto:peter.brawley@earthlink.net] Sent: Monday, August 27, 2007 10:18 PM To: Weston, Craig (OFT) Cc: mysql@lists.mysql.com Subject: Re: [MYSQL]Time formatting for cycle time. Craig, >I am working on Martin Minka's date diff function as found >at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful >thing. However, I am trying to alter it or identify a similar function >that instead of giving me the number of days between two dates it >returns the number of workday hours:minutes between two datetimes, or >some other date differential (such as an exact number of days between >two dates with remainder) Here's a logically equivalent datediff calc, mebbe slightly simpler: SET @d1 = '2007-1-1'; SET @d2 = '2007-3-31'; SET @dow1 = DAYOFWEEK(@d1); SET @dow2 = DAYOFWEEK(@d2); SET @days = DATEDIFF(@d2,@d1); SET @wknddays = 2 * FLOOR( @days / 7 ) + IF( @dow1 = 1 AND @dow2 > 1, 1, IF( @dow1 = 7 AND @dow2 = 1, 1, IF( @dow1 > 1 AND @dow1 > @dow2, 2, if( @dow1 < 7 AND @dow2 = 7, 1, 0 ) ) ) ); SELECT FLOOR(@days-@wkndDays) AS BizDays; To include time in the difference, you could adopt as a return convention a string format like 'N days hh:mm:ss', where N is the date difference calculated above, minus one if the time portion of d1 is later than than that of d2. Something like this: SET @d1 = '2007-1-1 00:00:00'; SET @d2 = '2007-3-31 12:00:00'; SET @tdiff = TIMEDIFF( TIME(@d1), TIME(@d2) ); SET @dow1 = DAYOFWEEK(@d1); SET @dow2 = DAYOFWEEK(@d2); SET @days = DATEDIFF(@d2,@d1); SET @wknddays = 2 * FLOOR( @days / 7 ) + IF( @dow1 = 1 AND @dow2 > 1, 1, IF( @dow1 = 7 AND @dow2 = 1, 1, IF( @dow1 > 1 AND @dow1 > @dow2, 2, IF( @dow1 < 7 AND @dow2 = 7, 1, 0 ) ) ) ); SET @days = FLOOR(@days - @wkndDays) - IF( @tdiff < 0, 1, 0 ); SET @tdiff = IF( ASCII(@tdiff) = 45, SUBSTRING(@tdiff,2), TIMEDIFF( '24:00:00', @tdiff )); SELECT CONCAT( @days, ' days ', @tdiff ); PB ----- Weston, Craig (OFT) wrote: Hello all, I am working on Martin Minka's date diff function as found at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful thing. However, I am trying to alter it or identify a similar function that instead of giving me the number of days between two dates it returns the number of workday hours:minutes between two datetimes, or some other date differential (such as an exact number of days between two dates with remainder) I hate asking open ended questions, but can anyone give me any hints as to how to make this conversion? I may be able to figure it out... In a few weeks. My long term goal is to identify the business hours(minutes, seconds, whatever) between two dates, taking into account weekends, holidays, and business hours. Thanks, Craig -------------------------------------------------------- This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. ________________________________ No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.484 / Virus Database: 269.12.9/975 - Release Date: 8/26/2007 9:34 PM |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Hi Peter,
It would be great if you'd post this snippet at the forge too. More is better :-) Baron Weston, Craig (OFT) wrote: > Wow, thanks. Lots to think about. > > > > ________________________________ > > From: Peter Brawley [mailto:peter.brawley@earthlink.net] > Sent: Monday, August 27, 2007 10:18 PM > To: Weston, Craig (OFT) > Cc: mysql@lists.mysql.com > Subject: Re: [MYSQL]Time formatting for cycle time. > > > > Craig, > > > >> I am working on Martin Minka's date diff function as found >> at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful >> thing. However, I am trying to alter it or identify a similar function >> that instead of giving me the number of days between two dates it >> returns the number of workday hours:minutes between two datetimes, or >> some other date differential (such as an exact number of days between >> two dates with remainder) > > Here's a logically equivalent datediff calc, mebbe slightly simpler: > > SET @d1 = '2007-1-1'; > SET @d2 = '2007-3-31'; > SET @dow1 = DAYOFWEEK(@d1); > SET @dow2 = DAYOFWEEK(@d2); > SET @days = DATEDIFF(@d2,@d1); > SET @wknddays = 2 * FLOOR( @days / 7 ) + > IF( @dow1 = 1 AND @dow2 > 1, 1, > > IF( @dow1 = 7 AND @dow2 = 1, 1, > IF( @dow1 > 1 AND @dow1 > @dow2, 2, > if( @dow1 < 7 AND @dow2 = 7, 1, 0 ) > ) > ) > ); > SELECT FLOOR(@days-@wkndDays) AS BizDays; > > To include time in the difference, you could adopt as a return > convention a string format like 'N days hh:mm:ss', where N is the date > difference calculated above, minus one if the time portion of d1 is > later than than that of d2. Something like this: > > SET @d1 = '2007-1-1 00:00:00'; > SET @d2 = '2007-3-31 12:00:00'; > SET @tdiff = TIMEDIFF( TIME(@d1), TIME(@d2) ); > SET @dow1 = DAYOFWEEK(@d1); > SET @dow2 = DAYOFWEEK(@d2); > SET @days = DATEDIFF(@d2,@d1); > SET @wknddays = 2 * FLOOR( @days / 7 ) + > IF( @dow1 = 1 AND @dow2 > 1, 1, > > IF( @dow1 = 7 AND @dow2 = 1, 1, > IF( @dow1 > 1 AND @dow1 > @dow2, 2, > IF( @dow1 < 7 AND @dow2 = 7, 1, 0 ) > ) > ) > ); > SET @days = FLOOR(@days - @wkndDays) - IF( @tdiff < 0, 1, 0 ); > SET @tdiff = IF( ASCII(@tdiff) = 45, SUBSTRING(@tdiff,2), TIMEDIFF( > '24:00:00', @tdiff )); > SELECT CONCAT( @days, ' days ', @tdiff ); > > PB > > ----- > > Weston, Craig (OFT) wrote: > > Hello all, > > I am working on Martin Minka's date diff function as found > at http://forge.mysql.com/snippets/view.php?id=56. It is a beautiful > thing. However, I am trying to alter it or identify a similar function > that instead of giving me the number of days between two dates it > returns the number of workday hours:minutes between two datetimes, or > some other date differential (such as an exact number of days between > two dates with remainder) > > > > > > I hate asking open ended questions, but can anyone give me any hints as > to how to make this conversion? I may be able to figure it out... In a > few weeks. > > > > My long term goal is to identify the business hours(minutes, seconds, > whatever) between two dates, taking into account weekends, holidays, and > business hours. > > > > Thanks, > > Craig > -------------------------------------------------------- > This e-mail, including any attachments, may be confidential, privileged > or otherwise legally protected. It is intended only for the addressee. > If you received this e-mail in error or from someone who was not > authorized to send it to you, do not disseminate, copy or otherwise use > this e-mail or its attachments. Please notify the sender immediately by > reply e-mail and delete the e-mail from your system. > > > > > > > ________________________________ > > > > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.5.484 / Virus Database: 269.12.9/975 - Release Date: > 8/26/2007 9:34 PM > > -- Baron Schwartz Xaprb LLC http://www.xaprb.com/ |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Ok, So, What I have come up with (so far) as a variant of Baron's query:
SET @d1 = '2007-2-1 00:00:00';# Start date SET @d2 = '2007-2-28 23:59:59';# End date SET @tdiff = TIMEDIFF( TIME(@d1), TIME(@d2) ); SET @dow1 = DAYOFWEEK(@d1); SET @dow2 = DAYOFWEEK(@d2); SET @days = DATEDIFF(@d2,@d1); SET @wknddays = 2 * FLOOR( @days / 7 ) + IF( @dow1 = 1 AND @dow2 > 1, 1, IF( @dow1 = 7 AND @dow2 = 1, 1, IF( @dow1 > 1 AND @dow1 > @dow2, 2, IF( @dow1 < 7 AND @dow2 = 7, 1, 0 ) ) ) ); SET @days2 = FLOOR(@days - @wkndDays) - (IF( @tdiff > 0, 1, 0 ) + (SELECT count(*) FROM `resource_data`.`holidays` WHERE `resource_data`.`holidays`.`date` BETWEEN @d1 AND @d2)); SET @tdiff = IF( ASCII(@tdiff) = 45, SUBSTRING(@tdiff,2), TIMEDIFF( '24:00:00', @tdiff )); SELECT CONCAT( @days2, ' days ', @tdiff ), @wknddays, @days, @days2, @tdiff, IF( @tdiff < 0, 1, 0 ),#Test value 1 IF( @tdiff > 0, 1, 0 ),#Test value 2 FLOOR(@days - @wkndDays), (SELECT count(*) FROM `holidays` WHERE `holidays`.`date` BETWEEN @d1 AND @d2); What I did was reverse the @tdiff equation to add a day for a positive @tdiff instead of subtracting it. My test month, February, has 2 holidays in it. The results: +------------------------------------+-----------+-------+ | CONCAT( @days2, ' days ', @tdiff ) | @wknddays | @days | +------------------------------------+-----------+-------+ | 17 days 23:59:59 | 8 | 27 | +------------------------------------+-----------+-------+ --------+------------------------+------------------------+----------+ @days2 | IF( @tdiff < 0, 1, 0 ) | IF( @tdiff > 0, 1, 0 ) | holidays | --------+------------------------+------------------------+----------+ 17 | 0 | 1 | 2 | --------+------------------------+------------------------+----------+ This seems to account for the remainder better? Of course the original reason it was added was to take into account times that were earlier, which it does not seem to do? Thank you for your ! Craig -------------------------------------------------------- This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Ok then,
Now I have a result set so very close to where I want to be. The query thus far: mysql> SET @d1 = '2007-02-01 18:24:04';# Start date SET @d2 = '2007-2-28 23:05:40';# End date set @wkldays = (select WorkDayDiff`(@d2,@d1)-1); Set @wkldays2 = if(@wkldays < 0,1,0); set @Day_End = (select `business_hours`.`Day_End` from `resource_data`.`business_hours` limit 1); set @Day_Start = (select `business_hours`.`Day_Start` from `resource_data`.`business_hours` limit 1); Set @t1 = (IF((HOUR(@d1))<@Day_Start,7,IF((HOUR(@d1))>@Day_E nd ,18,HOUR(@d1))))+(IF((HOUR(@d1))<@Day_Start,0,IF(( HOUR(@d1))>@Day_End ,0,MINUTE(@d1)))/60)+(IF((HOUR(@d1))<@Day_Start,0,IF((HOUR(@d1))>@D ay_En d ,0,SECOND(@d1)))/3600); Set @t2 = (IF((HOUR(@d2))<@Day_Start,7,IF((HOUR(@d2))>@Day_E nd ,18,HOUR(@d2))))+(IF((HOUR(@d2))<@Day_Start,0,IF(( HOUR(@d2))>@Day_End ,0,MINUTE(@d2)))/60)+(IF((HOUR(@d2))<@Day_Start,0,IF((HOUR(@d2))>@D ay_En d ,0,SECOND(@d2)))/3600); set @tdif = IF(@t1 > @t2,((22 - @t1) + (@t2 - 11)),@t2 - @t1); select @wkldays, @t1, @t2, @wkldays2, @tdif, ((@wkldays+@wkldays2)*11)+@tdif 'hours'; With the results: +----------+-----+-----+-----------+------- | @wkldays | @t1 | @t2 | @wkldays2 | @tdif +----------+-----+-----+-----------+------- | 17 | 18 | 18 | 0 | 0 +----------+-----+-----+-----------+------- +------------------------------------+ | hours | +------------------------------------+ | 187.000000000000000000000000000000 | +------------------------------------+ My question now, is does anyone know how I could alter this query to get precision in the hours? The idea is to get at least to the minute resolution. I tried ((@wkldays+@wkldays2)*11)+@tdif + 0.0000, but I have few brain cells not on strike. Thanks, Craig -------------------------------------------------------- This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Craig,
Right you are, here is a corrected func: DROP FUNCTION IF EXISTS BizDateTimeDiff; DELIMITER | CREATE FUNCTION BizDateTimeDiff( d1 DATETIME, d2 DATETIME ) RETURNS CHAR(30) DETERMINISTIC BEGIN DECLARE dow1, dow2, days, wknddays INT; DECLARE tdiff CHAR(10); SET dow1 = DAYOFWEEK(d1); SET dow2 = DAYOFWEEK(d2); SET tdiff = TIMEDIFF( TIME(d2), TIME(d1) ); SET days = DATEDIFF(d2,d1); SET wknddays = 2 * FLOOR( days / 7 ) + IF( dow1 = 1 AND dow2 > 1, 1, IF( dow1 = 7 AND dow2 = 1, 1, IF( dow1 > 1 AND dow1 > dow2, 2, IF( dow1 < 7 AND dow2 = 7, 1, 0 ) ) ) ); SET @tdiff = tdiff; SET days = FLOOR(days - wkndDays) - IF( ASCII(tdiff) = 45, 1, 0 ); SET tdiff = IF( ASCII(tdiff) = 45, TIMEDIFF( '24:00:00', SUBSTRING(tdiff,2)), TIMEDIFF( tdiff, '00:00:00' )); RETURN CONCAT( days, ' days ', tdiff ); END; | DELIMITER ; SELECT BizDateTimeDiff( '2007-1-1 00:00:00', '2007-3-31 00:00:00' ) AS dtdiff; SELECT BizDateTimeDiff( '2007-1-1 11:00:00', '2007-3-31 00:00:00' ) AS dtdiff; SELECT BizDateTimeDiff( '2007-1-1 12:00:00', '2007-3-31 13:00:00' ) AS dtdiff; SELECT BizDateTimeDiff( '2007-1-1 00:00:00', '2007-3-31 11:00:00' ) AS dtdiff; PB ----- Weston, Craig (OFT) wrote: > Ok, So, What I have come up with (so far) as a variant of Baron's query: > > > > SET @d1 = '2007-2-1 00:00:00';# Start date > SET @d2 = '2007-2-28 23:59:59';# End date > SET @tdiff = TIMEDIFF( TIME(@d1), TIME(@d2) ); > SET @dow1 = DAYOFWEEK(@d1); > SET @dow2 = DAYOFWEEK(@d2); > SET @days = DATEDIFF(@d2,@d1); > SET @wknddays = 2 * FLOOR( @days / 7 ) + > IF( @dow1 = 1 AND @dow2 > 1, 1, > IF( @dow1 = 7 AND @dow2 = 1, 1, > IF( @dow1 > 1 AND @dow1 > @dow2, 2, > IF( @dow1 < 7 AND @dow2 = 7, 1, 0 ) > ) > ) > ); > SET @days2 = FLOOR(@days - @wkndDays) - (IF( @tdiff > 0, 1, 0 ) + > (SELECT count(*) FROM `resource_data`.`holidays` WHERE > `resource_data`.`holidays`.`date` BETWEEN @d1 AND @d2)); > SET @tdiff = IF( ASCII(@tdiff) = 45, SUBSTRING(@tdiff,2), TIMEDIFF( > '24:00:00', @tdiff )); > SELECT CONCAT( @days2, ' days ', @tdiff ), > @wknddays, > @days, > @days2, > @tdiff, > IF( @tdiff < 0, 1, 0 ),#Test value 1 > IF( @tdiff > 0, 1, 0 ),#Test value 2 > FLOOR(@days - @wkndDays), > (SELECT count(*) FROM `holidays` WHERE `holidays`.`date` BETWEEN @d1 > AND @d2); > > > > What I did was reverse the @tdiff equation to add a day for a positive > @tdiff instead of subtracting it. My test month, February, has 2 > holidays in it. > > The results: > > +------------------------------------+-----------+-------+ > | CONCAT( @days2, ' days ', @tdiff ) | @wknddays | @days | > +------------------------------------+-----------+-------+ > | 17 days 23:59:59 | 8 | 27 | > +------------------------------------+-----------+-------+ > > > --------+------------------------+------------------------+----------+ > @days2 | IF( @tdiff < 0, 1, 0 ) | IF( @tdiff > 0, 1, 0 ) | holidays | > --------+------------------------+------------------------+----------+ > 17 | 0 | 1 | 2 | > --------+------------------------+------------------------+----------+ > > This seems to account for the remainder better? > > Of course the original reason it was added was to take into account > times that were earlier, which it does not seem to do? > > Thank you for your ! > Craig > -------------------------------------------------------- > This e-mail, including any attachments, may be confidential, privileged or otherwise legally protected. It is intended only for the addressee. If you received this e-mail in error or from someone who was not authorized to send it to you, do not disseminate, copy or otherwise use this e-mail or its attachments. Please notify the sender immediately by reply e-mail and delete the e-mail from your system. > > > |
|
![]() |
| Outils de la discussion | |
|
|