简体   繁体   English

PHP,sql查询到CSV,格式化总计行

[英]PHP, sql query to CSV, format rows for totals

I have a working script that, when executed, runs the following SQL query which is then written to a CSV. 我有一个工作脚本,该脚本在执行时会运行以下SQL查询,然后将其写入CSV。 All of this works perfectly. 所有这些完美地工作。

However, I'd like to adjust the formatting somewhat. 但是,我想稍微调整一下格式。 It currently writes results by employee and orders by last night. 目前,它按员工和订单记录结果,直到昨晚。 I'd like to have a row at the end of all agent's records that said something like "Agent's Name: Totals" and then sums up the totals of their records. 我想在所有座席记录的末尾一行,写上“ Agent's Name:Totals”之类的内容,然后对他们的记录总数求和。

For example, each row has their name, phone extension, and then several metrics which are either blank or have an 'x', and lastly a number representing time on the phone. 例如,每行都有其名称,电话分机,然后是几个度量标准,这些度量标准为空白或带有“ x”,最后是一个数字,代表电话的时间。 So I'd like to total the x's in the appropriate fields, add the duration of time on the phone, and lastly a count of total calls (which would be a count of that employee's records). 因此,我想在适当的字段中总计x,在电话上添加持续时间,最后是总通话次数(这将是该员工的记录数)。

I don't know if this should be done in the SQL, in the CSV code block or if there's a better way to store the metrics with PHP and do this programmatically. 我不知道这是否应该在SQL中,在CSV代码块中完成,或者是否有更好的方法用PHP存储指标并以编程方式进行。

I'm a rookie here, normally just relying on queries in MySQL workbench, so any help is much appreciated. 我是这里的菜鸟,通常仅依赖于MySQL工作台中的查询,因此非常感谢您的帮助。

Here's the script: 这是脚本:

$result = mysqli_query($conn2,
"SELECT
      firstn
    , lastn
    , extension
    , Recieved
    , RecievedKnown
    , Outbound
    , outboundKnown
    , Missed
    , MissedKnown
    , CallingNumber
    , CalledNumber
    , starttime
    , endtime
      , duration
    , HOLDTIMESECS
    , TERMINATIONREASONCODE

FROM (
      SELECT
              u.firstn
            , u.lastn
            , c.extension
            , CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
            , CASE WHEN LEGTYPE1 = 2 AND answered = 1 AND CALLINGPARTYNO = k.phone_number THEN 'x' ELSE '' END AS RecievedKnown
            , CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 THEN 'x' ELSE '' END  AS Outbound
            , CASE WHEN LEGTYPE1 = 1 AND FINALLYCALLEDPARTYNO = k.phone_number THEN 'x' ELSE '' END AS outboundKnown
            , CASE WHEN Answered = 0 THEN 'x' ELSE '' END AS Missed
            , CASE WHEN ANSWERED = 0 AND CALLINGPARTYNO = k.phone_number THEN 'x' ELSE '' END AS MissedKnown
            , a.CALLINGPARTYNO AS CallingNumber
            , a.FINALLYCALLEDPARTYNO AS CalledNumber
            , b.starttime AS starttime
            , b.endtime AS endtime
            , b.duration
            , a.holdtimesecs
            , a.terminationreasoncode
      FROM ambition.session a
      INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
      INNER JOIN ambition.mxuser c ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
      INNER JOIN jackson_id.users u ON c.extension = u.extension
      LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
      WHERE date(b.ts) >= curdate()
      AND LEGTYPE1 <> 12 -- This keeps the report from having blank spaces due to the 12 legtype.
      AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)

      ) x
    ORDER BY lastn") or die(mysqli_error( $conn2));



    $userDetails = array();
    while($row = $result->fetch_array(MYSQLI_NUM)){
        /* Let me take the extension as the key to store its respective counts */
        $extension = $row['extension'];
        /* Now in your while loop do all the calculations for the user */
        if(!isset($userDetails[$extension])){
            /* Since this is the first time for the extension your doin the calculations */
            $userDetails[$extension]['missedCallCounts'] = 1; /* First time count */
        }else{
            $userDetails[$extension]['missedCallCounts'] += 1; /* Sum up the count */
        }
    }

    foreach($userDetails as $userDetail){
/* In the following line dump the respective userdetails to csv which will show summary */
fputcsv($fp, array_values($userDetails));
}

And a screenshot of the current CSV results in excel (omitted names/numbers, but you still see the idea): 当前CSV的屏幕截图显示为excel(省略了名称/数字,但您仍然看到了这个主意): 在此处输入图片说明

So for extension 7312, under the first two rows would be a new row with something like: 因此,对于扩展名7312,在前两行下将是一个新行,其内容如下:

7312's Totals | 0 | 0 | 0 | 0 | 2 | 2 | - | - | - | - | 76 | 0

Your looping into the sql query to output the same to CSV file, at that time keep the records in generic array and dump once the loop gets over. 循环到sql查询以将其输出到CSV文件,这时将记录保留在通用数组中,并在循环结束后转储。

For Eg: 例如:

$userDetails = array();
while($row = $result->fetch_array(MYSQLI_NUM)){
    /* Let me take the extension as the key to store its respective counts */
    $extension = $row['extension'];
    /* Now in your while loop do all the calculations for the user */
    if(!isset($userDetails[$extension])){
        /* Since this is the first time for the extension your doin the calculations */
        $userDetails[$extension]['missedCallCounts'] = 1; /* First time count */
    }else{
        $userDetails[$extension]['missedCallCounts'] += 1; /* Sum up the count */
    }
}

Now loop into the $userDetails and dump the same to CSV 现在循环到$ userDetails并将其转储到CSV

foreach($userDetails as $userDetail){
    /* In the following line dump the respective userdetails to csv which will show summary */
    fputcsv($fp, array_values($userDetails));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM