0

我对 postgres 数据库有多个查询,我希望将数据返回到该数据库。当前在两列中的几行上返回数据,这是错误的。我希望数据显示在一行和几列中。查询是这样的:

<?php

$sql = "Select ceil(SUM (a.CALLDURATION::integer) / 60) AS minutes,  sum(a.alltaxcost::integer) AS revenue
FROM cdr_data a,COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')

and not substr(a.zoneiddest,1,3) in ('254','255','211','257','250','256')
and trim(a.zoneiddest)  = trim(b.country_code)

union

select  ceil(sum(callduration::integer/60) )as    total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data 
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and  regexp_replace(callednumber,'^256','') ~ '^73'
and bundleunits = 'Money'
and regexp_replace(callednumber,'^256','') ~ '^73'

union

select ceil(sum(callduration::integer/60) )as  total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data 
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and  identifiant ~'^73'
and bundleunits = 'Money'
and zoneorange <> '-1'

union

SELECT sum(a.alltaxcost::integer) AS revenue, ceil(SUM (a.CALLDURATION::integer) / 60)  AS minutes
FROM cdr_data a,  COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')

and  substr(a.zoneiddest,1,3) in ('254','255','211','257','250')
and trim(a.zoneiddest)  = trim(b.country_code)
";


$result = pg_query($dbh, $sql);
if (!$result) {
 die("Error in SQL query: " . pg_last_error());
 }


  while ($row = pg_fetch_array($result)) {
  echo "<tr style='background-color: #FFFFFF;font-size:15px' align='center'>";
     echo "<td>" . $row[0] . "</td>";
     echo "<td>" . $row[1] . "</td>";
     echo "<td>" . $row[2] . "</td>";
     echo "<td>" . $row[3] . "</td>";
     echo "<td>" . $row[4] . "</td>";
     echo "<td>" . $row[5] . "</td>";
     echo "<td>" . $row[6] . "</td>";
     echo "<td>" . $row[7] . "</td>";
     echo "<td>" . $row[8] . "</td>";
     echo "<td>" . $row[9] . "</td>";
     echo "<td>" . $row[10] . "</td>";
     echo "<td>" . $row[11] . "</td>";

 echo "</tr>";
    }
 // free memory
 pg_free_result($result);       

 // close connection
 pg_close($dbh);
 ?>

这将我的数据显示为:

International Minutes   International Revenue   Onnet Minutes   Onnet Revenue                        
      0                         0                                       
     1343                         578086                                        

这不是我想要的。我希望结果是这样的

International Minutes   International Revenue   Onnet Minutes     Onnet Revenue                      
      0                         0              1343                  578086                                 

所以我希望我的结果在一行和超过 10 列中返回。我怎样才能做到这一点。

4

3 回答 3

1

因此,不要执行 UNION,而是将每一列设为自己的 select 语句。请注意,这可能不是很有效。另一种选择是插入另一个表或视图,然后从中查询。

这只是一个例子,但类似这样:

$sql = "Select 
          (Select ceil(SUM (a.CALLDURATION::integer) / 60)       
          FROM cdr_data a,COUNTRY_CODES b
          WHERE  a.CALLSUBCLASS = '002'
          AND a.CALLCLASS = '008'
          and a.zoneiddest::integer > 0
          AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
          ('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
          and not substr(a.zoneiddest,1,3) in ('254','255','211','257','250','256')
          and trim(a.zoneiddest)  = trim(b.country_code) as International Minutes
         ,(Select ...
           from ...
           where ...) as International Revenue
         ,(Select ... 
           from ...
           where) as Onnet Minutes
         ,(Select ...
           from ...
           where ...) as Onnet Revenue
         FROM ...
         WHERE...
         ";
于 2013-01-28T14:34:57.440 回答
0

有一个名为 tablefunc 的 postgresql 扩展,其中包含函数crosstab完全符合您的需要。请参阅文档页面:http ://www.postgresql.org/docs/9.1/static/tablefunc.html ;

您需要先安装此扩展程序:

CREATE EXTENSION tablefunc;
于 2013-01-28T10:54:51.213 回答
0

我不确定,但你可以试试这个。

 echo "<tr style='background-color: #FFFFFF;font-size:15px' align='center'>";

 while ($row = pg_fetch_array($result)) 
 {
    echo "<td>" . $row[0] . "</td>";
    echo "<td>" . $row[1] . "</td>";
    echo "<td>" . $row[2] . "</td>";
    echo "<td>" . $row[3] . "</td>";
    echo "<td>" . $row[4] . "</td>";
    echo "<td>" . $row[5] . "</td>";
    echo "<td>" . $row[6] . "</td>";
    echo "<td>" . $row[7] . "</td>";
    echo "<td>" . $row[8] . "</td>";
    echo "<td>" . $row[9] . "</td>";
    echo "<td>" . $row[10] . "</td>";
    echo "<td>" . $row[11] . "</td>";
}

echo "</tr>";
于 2013-01-28T10:51:46.507 回答