MySQL/PHP get average of past two months based on variable month -


i'm trying average of value past 2 months, not based on curdate()/now(). dependent on month user looking @ in application:

ex: if i'm looking @ may, want average of april , march. if i'm looking @ february, want average of january , december (of previous year).

i have function accepts month , year of page user on (it accepts emp_id, irrelevant question).

public function getprotectedamt($month,$year,$id){ $query = "select avg(total_payout) avg_payout saved_plan_data emp_id = '$id' , //this dont know query for"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); return $row['avg_payout']; } 

in table saved_plan_data, there fields plan_month , plan_year store int values of month/year employee's total_payout saved for.

how write clause avg previous 2 months, depending on values of $month , $year are?

i think easier , more readable solution one:

select ... ...     (plan_year, plan_month) in ((2012, 12), (2013, 1)) 

you need compute appropriate values.

this quite readable:

where concat_ws('-', plan_year, plan_month) in ('2012-12', '2013-01') 

Comments

Popular posts from this blog

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

Android Gson.fromJson error -