在postgresql中将行计数计算为多列

时间:2016-03-07 08:21:48

标签: postgresql

我有如下表格

public function sendbulkemail($email,$subject,$message)
    {
        $adminEmail = Yii::app()->params['adminEmail'];
        $message = wordwrap($message, 70);
        $message = str_replace("\n.", "\n..", $message);
       // $mail2 = new PHPMailer(); // create a new object

        $mail=Yii::app()->Smtpmail;
        $mail->SetFrom($adminEmail, '');
        $mail->Subject = '=?UTF-8?B?'.base64_encode($subject).'?=';
        $mail->MsgHTML($message);
        $mail->AddAddress($email, "");
        $mail->SMTPSecure = 'tls';
        $mail->Send();
        $mail->ClearAddresses();
    }

因此,我需要3列包含;

  1. “src_dn”列中有多少“call_id”组具有“10001”
  2. 在“dst_dn”列
  3. 中有多少“call_id”组具有“10001”
  4. 当“seg_order”为最大值时(“最后一段”),“call_id”组在“dst_dn_type”中有“6”的数量
  5. 对于第一和第二,“10001”应该是每组计数一次。

    由于

    修改 预期结果

    +---------+-----------+---------+-------------+--------+
    | call_id | seg_order | src_dn  | dst_dn_type | dst_dn |
    +---------+-----------+---------+-------------+--------+
    |   29192 |         1 | "8004"  |           0 | "2215" |
    |   29193 |         1 | "8000"  |           6 | "2239" |
    |   29194 |         1 | "10001" |           6 | "8802" |
    |   29194 |         2 | "10001" |           6 | "8802" |
    |   29194 |         3 | "10001" |           4 | "8003" |
    |   29194 |         4 | "10001" |           4 | "8003" |
    |   29194 |         5 | "8003"  |           0 | "2225" |
    |   29194 |         6 | "8003"  |           0 | "2225" |
    |   29194 |         7 | "10001" |           0 | "2225" |
    |   29195 |         1 | "10000" |           6 | "8857" |
    |   29195 |         2 | "10000" |           6 | "8857" |
    |   29195 |         3 | "10000" |           4 | "8002" |
    |   29195 |         4 | "10000" |           4 | "8002" |
    |   29195 |         5 | "8002"  |           0 | "2213" |
    |   29195 |         6 | "8002"  |           0 | "2213" |
    |   29195 |         7 | "10000" |           0 | "2213" |
    |   29196 |         1 | "10002" |           6 | "8800" |
    |   29196 |         2 | "10002" |           6 | "8800" |
    |   29196 |         3 | "10002" |           4 | "8000" |
    |   29196 |         4 | "10002" |           4 | "8000" |
    |   29196 |         5 | "8000"  |           0 | "2240" |
    |   29196 |         6 | "8000"  |           0 | "2240" |
    |   29196 |         7 | "10002" |           0 | "2240" |
    |   29197 |         1 | "10003" |           6 | "8804" |
    |   29198 |         1 | "8000"  |           0 | "2240" |
    |   29199 |         1 | "8004"  |           0 | "2220" |
    |   29200 |         1 | "8004"  |           0 | "2213" |
    |   29201 |         1 | "10002" |           6 | "8800" |
    |   29202 |         1 | "10003" |           6 | "8804" |
    |   29202 |         2 | "10003" |           6 | "8804" |
    |   29202 |         3 | "10003" |           2 | "8010" |
    |   29202 |         4 | "10003" |           4 | "8004" |
    |   29202 |         5 | "10003" |           4 | "8004" |
    |   29202 |         6 | "8004"  |           0 | "2215" |
    |   29202 |         7 | "8004"  |           0 | "2215" |
    |   29202 |         8 | "10003" |           0 | "2215" |
    +---------+-----------+---------+-------------+--------+
    

    编辑 到目前为止我做了什么

    此查询适用于(我相信)第一步和第二步

    +---------+-----------+---------+
    | inbound | outbound  | ivr     |
    +---------+-----------+---------+
    |      1  |        0  |      1  |
    +---------+-----------+---------+
    

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的:

SELECT
    COALESCE((SELECT count(DISTINCT call_id) FROM tbl WHERE src_dn = '10001'), 0) AS inbound,
    COALESCE((SELECT count(DISTINCT call_id) FROM tbl WHERE dst_dn = '10001'), 0) AS outbound,
    COALESCE((SELECT count(DISTINCT call_id) FROM tbl WHERE dst_dn_type = 6 
        AND seg_order = (select max(seg_order) from tbl)), 0) AS outbound;

COALESCE功能是返回0.您可以在此找到它的工作原理:http://www.postgresql.org/docs/9.3/static/functions-conditional.html