我在特定时间段内提取了多个状态,我想在此期间提取另一个“每日平均销售额”的统计数据。我不确定如何在特定时间段内做每日平均值,有人可以提供一些建议吗?
$whereSql = 'WHERE created >= '.$endTimestamp.' AND
created <= '.$startTimestamp;
$tru->query->run(array(
'name' => 'get-figures',
'sql' => 'SELECT
SUM(price) AS total_sales,
COUNT(id) AS total_orders,
AVG(total) AS order_total_average
(SELECT
SUM(quantity)
FROM `order_product`
INNER JOIN `order` ON (
`order`.id = order_product.order_id AND
`order`.created >= '.$endTimestamp.' AND
`order`.created <= '.$startTimestamp.' AND
`order`.type_id = '.$type->getId().'
)
) as total_units
FROM `order`
'.$whereSql.' AND type_id = '.$type->getId().'',
'connection' => 'store'
));
答案 0 :(得分:0)
您需要按范围限定日期的to_char进行分组(也许是'ddd') 并且您将需要一个限制到正确日期范围的where子句组件
答案 1 :(得分:0)
如果我理解您的需要,您可以将总销售额除以所选时间段内的天数。将此列添加到外部select语句:
SUM(price)/DATEDIFF($endTimestamp,$startTimestamp)
还要确保使用mysql_real_escape_string转义用户提供的值,否则会出现SQL注入漏洞。