0

データを返す postgres データベースへの複数のクエリがあります。現在、データは 2 列の複数の行で返されていますが、これは間違っています。データを 1 行と複数の列で表示したいと考えています。クエリは次のようになります。

<?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                                 

したがって、結果が 1 行 10 列以上で返されるようにしたいと考えています。どうすればこれを達成できますか。

4

3 に答える 3

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 に答える
0

まさに必要なことを行う関数crosstabを含む tablefunc という名前の postgresql 拡張機能があります。ドキュメント ページを参照してください: http://www.postgresql.org/docs/9.1/static/tablefunc.html ;

この拡張機能を最初にインストールする必要があります。

CREATE EXTENSION tablefunc;
于 2013-01-28T10:54:51.213 に答える