在Sql / php脚本中创建更具体的报告

时间:2017-12-20 16:21:22

标签: php mysql excel csv

我在php脚本中运行了一个相当密集的SQL查询,它基本上汇总了我们每个员工在电话上的号码,创建了一个CSV,然后通过PHP邮件程序将其附加到电子邮件中。

这种方法很好,但我的任务是把它变成一份完整的报告(即excel报告,格式化等),并为每位员工提供总计。现在它基本上只有每个员工的行项目,因此员工A可能有32个记录,然后它直接进入员工B.我需要更改它,以便在员工A的记录结束时,它插入一行总计每列的值。

我想我只能使用PHP来做这件事,因为这是一个PHP脚本,但我不知道这里最好的方法,我希望得到一些指导,我可以适用于查询的其余部分。

我认为为字段/列创建一个变量可能会很好,这样我就可以用这种方式创建一个SUM,但我对此表示满意。

这是脚本的SQL部分:

$result = mysqli_query($conn2,
    "SELECT
        FirstN
        , LastN
        , Extension
        , Recieved
        , Recieved_Known
        , Outbound
        , Outbound_Known
        , Missed_No_VM
        , Missed_VM
        , Missed_Known
        , Calling_Number
        , Called_Number
        , Start_Time
        , End_Time
        , Talk_Time_Seconds
        , Hold_Time_Seconds

        FROM (
          SELECT distinct
           firstn
           , 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 Recieved_Known
           , CASE WHEN LEGTYPE1 = 1 then 'x' ELSE '' end  AS Outbound
           , case when  FINALLYCALLEDPARTYNO = kn.long_number then 'x' ELSE '' end as Outbound_Known
           , case when legtype1 = 2 and answered = 0 and finallycalledpartyno  not like '%oice%' then 'x' ELSE '' end as Missed_No_VM
           , case when finallycalledpartyno like '%oice%' then 'x' ELSE '' end as Missed_VM
           , case when legtype1 = 2 and ANSWERED = 0 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as
            Missed_Known
           , a.CALLINGPARTYNO AS Calling_Number
           , a.FINALLYCALLEDPARTYNO AS Called_Number
           , b.starttime as Start_Time
           , b.endtime as End_Time
           , b.duration as Talk_Time_Seconds
           , a.holdtimesecs as Hold_Time_Seconds

          FROM ambition.session a
            INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
            right join jackson_id.users c on a.callingpartyno = c.extension or a.finallycalledpartyno = c.extension
            LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
        left join ambition.known_numbers kn on a.finallycalledpartyno = kn.long_number
                WHERE a.ts >= '2017-12-07' -- curdate()
                and(a.CALLINGPARTYNO in (select extension from ambition.ambition_users) OR a.finallycalledpartyno IN (select extension from ambition.ambition_users))
          ) x
          order by extension;") or die(mysqli_error( $conn2));

当前CSV的屏幕截图: this GitHub issue

正如您所看到的,它从代理7200发送到代理7206.但我需要在每列中插入一行总计那些x。

1 个答案:

答案 0 :(得分:0)

尝试https://github.com/PHPOffice/PHPExcel库,它非常适合创建Excel文件,您可以添加公式来进行添加。希望有所帮助!