how to get quarter, half year, yearly and 24 months reports in mysql?

Viewed 1412

I am working on requirement of generating reports for every 1, 3, 6, 12, and 24 months respectively where user selects his choice by a select dropdown. First I wrote query to generate report for 1 month which was worked fine. But I am not sure how to make that selection dynamic based on user selection.

My query is below:

switch($months){
    case "1":
        $stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND MONTH(track_date)=MONTH(CURDATE())");
        break;
    case "3":
        $stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 3)");
        break;
    case "6":
        $stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 6)");
        break;
    case "12":
        $stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 12)");
        break;
    case "24":
        $stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 24)");
        break;
}

My Select dropdown is:

<select name="month_analysis" id="month_analysis" class="month_analysis">
            <option value="1" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='1')?'selected':''?>>Current Month</option>
            <option value="3" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='3')?'selected':''?> >3 Months</option>
            <option value="6" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='6')?'selected':''?> >6 Months</option>
            <option value="12" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='12')?'selected':''?> >12 Months</option>
            <option value="24" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='24')?'selected':''?> >24 Months</option>
</select>

Presently, The query seems to be working but I am not sure if it is working fine or not. Please any suggestions/help? Thanks.

1 Answers
Related