Jump to content

How to get average value based on user selection?


xhkmx

Recommended Posts

I manage to display data using below query that will show value for each month. How to calculate average value starting july onwards(based on user selection).

 

Let say the value for element 'A'  are Jul=80, Aug= 80, Sep=90. If user select Aug then the average is 80 and if user select Sep then the average is 83.

 

Query:

$sql2 = "Select element,
	SUM(CASE WHEN Month = 'Jan' THEN total ELSE 0 END ) AS Jan,
	SUM(CASE WHEN Month = 'Feb' THEN total ELSE 0 END ) AS Feb,
	SUM(CASE WHEN Month = 'Mac' THEN total ELSE 0 END ) AS Mac,
	SUM(CASE WHEN Month = 'Apr' THEN total ELSE 0 END ) AS Apr,
	SUM(CASE WHEN Month = 'May' THEN total ELSE 0 END ) AS May,
	SUM(CASE WHEN Month = 'Jun' THEN total ELSE 0 END ) AS Jun,
	SUM(CASE WHEN Month = 'Jul' THEN total ELSE 0 END ) AS Jul,
	SUM(CASE WHEN Month = 'Aug' THEN total ELSE 0 END ) AS Aug,
	SUM(CASE WHEN Month = 'Sep' THEN total ELSE 0 END ) AS Sep,
	SUM(CASE WHEN Month = 'Oct' THEN total ELSE 0 END ) AS Oct,
	SUM(CASE WHEN Month = 'Nov' THEN total ELSE 0 END ) AS Nov,
	SUM(CASE WHEN Month = 'Dec' THEN total ELSE 0 END ) AS Dis
	FROM tbl_ma GROUP BY element";
	$rs2 = mysql_query($sql2);
	$getRec2 = mysql_fetch_assoc($rs2);

Below is how print the data:

<?php 
while ($row = mysql_fetch_assoc($rs2)) {
?>
<tr>
<td style="color:black;background-color:#f5efef"><div align="left"><?php echo $row['element']; ?></div></td>
<?php if( $year != '2014' ){ ?>
<?php if( 1 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Jan']; ?></div></td><?php endif; ?>
<?php if( 2 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Feb']; ?></div></td><?php endif; ?>
<?php if( 3 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Mac']; ?></div></td><?php endif; ?>
<?php if( 4 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Apr']; ?></div></td><?php endif; ?>
<?php if( 5 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['May']; ?></div></td><?php endif; ?>
<?php if( 6 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Jun']; ?></div></td><?php endif; ?>
<?php } ?>
<?php if( 7 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Jul']; ?></div></td><?php endif; ?>
<?php if( 8 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Aug']; ?></div></td><?php endif; ?>
<?php if( 9 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Sep']; ?></div></td><?php endif; ?>
<?php if( 10 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Oct']; ?></div></td><?php endif; ?>
<?php if( 11 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Nov']; ?></div></td><?php endif; ?>
<?php if( 12 <= $monthID ): ?><td><div align="center" style="color:black"><?php echo $row['Dis']; ?></div></td><?php endif; ?>
<td><div align="center" style="color:black"><?php //echo average; ?></div></td>
</tr>                                  
<?php } ?>

 

 

Edited by xhkmx
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.