php - SQL query count for each specified date -
i have sql query running on mysql database looks this...
$count0 = $wpdb->get_results(" select *, count(*) totalvaluecount wp_rg_lead inner join wp_rg_lead_detail on wp_rg_lead.id=wp_rg_lead_detail.lead_id wp_rg_lead.form_id = '46' , cast(date_created date) >= current_date - interval '7' day , field_number = '18' group value "); foreach ( $count0 $page ) { echo $repid_field . ' - ' . $page->form_id . ' - ' . $page->value . ' - ' . $page->lead_id . ' - ' . $page->date_created.' - ' . $page->totalvaluecount. '<br/>';
what want return count each value each of previous 7 days, can results this...
value | 01/01/2014 | 01/01/2014 | 01/01/2014 -------------------------------------------------- aa01 34 23 12 br65 3 65 65 yr76 45 12 65
is need php function afterwards or can done sql query itself?
this 1 should job
select value, sum(case when cast(date_created date) = current_date - interval '7' day 1 else 0 end ) d7, ... sum(case when cast(date_created date) = current_date - interval '1' day 1 else 0 end ) d1 wp_rg_lead inner join wp_rg_lead_detail on wp_rg_lead.id=wp_rg_lead_detail.lead_id wp_rg_lead.form_id = '46' , cast(date_created date) >= current_date - interval '7' day , field_number = '18' group value
Comments
Post a Comment