我有一个用户表,我需要在其中运行报告并显示给定年份每个月用户进入系统的次数。我创建了一个表,其中第一列返回不同的用户。接下来,在右侧,我想显示每个月用户输入了多少次。我目前拥有的代码只计算每个月的总数,并将每个月的总数放入所有用户。在下面的案例中,它为每个用户列出了 5 月的 7 个、6 月的 13 个和 7 月的 12 个。但我希望它显示如下,它按月分解。
Users May June July
User1 1 2 3
User2 1 1 2
User3 5 10 7
<?php
$sql = "SELECT record,count(*) as TotalCount,
(select DISTINCT count(*) from Table1 WHERE datereceived BETWEEN '2012-05-01' AND '2012-05-31') as May,
(select DISTINCT count(*) from Table1 WHERE datereceived BETWEEN '2012-06-01' AND '2012-06-30') as June,
(select DISTINCT count(*) from Table1 WHERE datereceived BETWEEN '2012-07-01' AND '2012-07-31') as July
FROM Table1 GROUP BY record";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
?>
<tr id="styling">
<td class="style"><?php echo $row['record']; ?></td>
<td class="style"><?php echo $row['May']; ?></td>
<td class="style"><?php echo $row['June']; ?></td>
<td class="style"><?php echo $row['July']; ?></td>
</tr>
<?php } ?>