I have these 4 queries repeated 3 times for each agent. Is there anyway to simplify/combine these queries? I don't mind using a while loop for the sums. The only thing that changes are the dates.
$john_week_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND date >= '$monday' AND rvp ='john smith'"),0);
$john_month_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND date >= '$this_month' AND rvp ='john smith'"),0);
$john_year_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND date >= '$this_year' AND rvp ='john smith'"),0);
$john_total = mysql_result(mysql_query("SELECT SUM(tp) FROM info WHERE type='life' AND rvp ='john smith'"),0);
You can have multiple SUM
aggregators in the field list
SELECT
SUM(IF(date >= '$monday' AND rvp = 'john smith'), tp, 0) AS john_week_total
SUM(IF(date >= '$this_month' AND rvp = 'john smith'), tp, 0) AS john_month_totalo
-- etc.
FROM
info
WHERE
type = 'life'
Your code is vulnerable to injection. You should use properly parameterized queries with PDO or mysqli
Does a query of this sort help you?
select
sum(case when date >= '$monday' then tp else 0 end) as weektotal,
sum(case when date >= '$this_month' then tp else 0 end) as monthtotal,
sum(case when date >= '$this_year' then tp else 0 end) as yeartotal,
sum(tp) as alltotal
from info
where type = 'life'
and rvp = 'john smith'