Created
March 24, 2014 23:23
Ask Ben: Averaging Date/Time Stamps In SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CAST( date_time_value AS FLOAT ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfquery name="qFullAverage" datasource="..." username="..." password="..."> | |
SELECT | |
CAST( | |
AVG( | |
CAST( date_created AS FLOAT ) | |
) | |
AS DATETIME | |
) | |
FROM | |
web_stats_session | |
</cfquery> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfquery name="qDateOnlyAverage" datasource="..." username="..." password="..."> | |
SELECT | |
CAST( | |
FLOOR( | |
AVG( | |
-- This inner FLOOR() call is optional (I think). | |
-- The averaging of the integer parts should | |
-- not be affected. Put it in if you want. | |
-- FLOOR( | |
CAST( date_created AS FLOAT ) | |
--) | |
) | |
) | |
AS DATETIME | |
) | |
FROM | |
web_stats_session | |
</cfquery> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfquery name="qTimeOnlyAverage" datasource="..." username="..." password="..."> | |
SELECT | |
CAST( | |
AVG( | |
-- Get full floating point value. | |
CAST( date_created AS FLOAT ) - | |
-- Subtract the integer part. | |
FLOOR( | |
CAST( date_created AS FLOAT ) | |
) | |
) | |
AS DATETIME | |
) | |
FROM | |
web_stats_session | |
</cfquery> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment