#
#  This query properly handles calls that span from the
#  previous reset period into the current period but
#  involves more work for the SQL server than those
#  below
#
query = "\
	SELECT SUM(acctsessiontime - GREATEST((%%b - UNIX_TIMESTAMP(acctstarttime)), 0)) \
	FROM radacct \
	WHERE username = '%{${key}}' \
	AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%%b'"

#
#  This query ignores calls that started in a previous
#  reset period and continue into into this one. But it
#  is a little easier on the SQL server
#
#query = "\
#	SELECT SUM(acctsessiontime) \
#	FROM radacct \
#	WHERE username = '%{${key}}' \
#	AND acctstarttime > FROM_UNIXTIME('%%b')"

#
#  This query is the same as above, but demonstrates an
#  additional counter parameter '%%e' which is the
#  timestamp for the end of the period
#
#query = "\
#	SELECT SUM(acctsessiontime) \
#	FROM radacct \
#	WHERE username = '%{${key}}' \
#	AND acctstarttime BETWEEN FROM_UNIXTIME('%%b') AND FROM_UNIXTIME('%%e')"

#
#  This query allows retrieving the entries based on a
#  period that resets on a particular day of the month.
#
#reset_day = 21
#query = "\
#	SELECT SUM(acctsessiontime) FROM radacct WHERE username = '%{${key}}' AND \
#		IF (DAY(CURDATE()) >= ${reset_day}, \
#			acctstarttime > DATE(DATE_FORMAT(NOW(), '%Y-%m-${reset_day}')), \
#			acctstarttime > DATE(DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-${reset_day}')) \
#		)"
#
