PHP SQL 中的 PIVOT 脚本

PHP script for PIVOT in SQL

我的 SQL 服务器中有一个视图 Emp_PRJ_Art,如下所示。

UsedArticleNo | EmpName | Project | EmpRol  
PART0001    |    Tintin | PRJ1    |   PL  
PART0002    |   Haddok  | PRJ2    |   TL  
PART0003    |   Poppey  | PRJ3    |   GET  
PART0004    |   Archie  | PRJ4    |   PM  
PART0005    |   Tintin  | PRJ1    |   PL  
PART0006    |   Archie  | PRJ4    |   PM  
PART0007    |   Tintin  | PRJ3    |   PL  
PART0008    |   Haddok  | PRJ3    |   TL  
PART0009    |   Poppey  | PRJ1    |   GET  

我想得到这样的结果。 PRJ 列下的数字是员工在该特定项目中使用的文章总数。

EmpName |PRJ1 | PRJ2 |  PRJ3 | PRJ4 |  EmpRol  
Archie  | 0   |  0   |   0   |  2   |  PM  
Haddok  | 0   |  1   |   1   |  0   |  TL  
Poppey  | 1   |  0   |   1   |  0   |  GET  
Tintin  | 2   |  0   |   1   |  0   |  PL  

我像这样使用 SQL 查询并根据需要获得结果。

SELECT *   
FROM ( SELECT EmpName, UsedArticleNo, Project  
        FROM Emp_PRJ_Art)  
PIVOT(COUNT(UsedArticleNo) FOR (Project) IN ('PRJ1','PRJ2','PRJ3',’PRJ4’))  
ORDER BY EmpName;

现在我的问题是,我无法使用 PHP 获取此查询 运行 并最终显示与网页相同的 table,因为我正在进入 SQL 开发者控制台。下面是 PHP 代码不起作用。

    <?php

$backcol="bgcolor=#9fffa1"; // Light Green
$colgreen1="bgcolor=#5EE060"; // Light Green

$maxitems=1000; // Maximum items found to display

$s=" <small> ";

$user='odbc';
$pass='abcd';
$sid ='server1.myComp.com';

$adr = "http://server1/WebEditor";

$conn = oci_connect($user, $pass, $sid);
if (!$conn) {
    echo "Unable to connect: " .  var_dump(OCIError()  );
    die();
}

}

$cmd="SELECT * 
FROM ( SELECT EmpName, Used ArticleNo, Project  FROM Emp_PRJ_Art)
PIVOT(COUNT(ArticleNo) FOR (Project) IN ('PRJ1','PRJ2','PRJ3','PRJ4'))
ORDER BY Emp Name";
$stid = oci_parse($conn,$cmd); oci_execute($stid);
echo     "<tr>
            <th $backcol> $s Employee Name
            <th $backcol> $s Project-1
            <th $backcol> $s Project-2
            <th $backcol> $s Project-3
            <th $backcol> $s Project-4
            <th $backcol> $s EmpRol
             ";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

    echo "<tr>\n";
    echo "<td> $s " .$row['EmpName']
        ."<td> $s " .$row['PRJ1']
        ."<td> $s " .$row['PRJ2']
        ."<td> $s " .$row['PRJ3']
        ."<td> $s " .$row['PRJ4']
        ."<td> $s " .$row['EmpRol']
        ;
} 
echo "</table>\n";
echo "<br><font color='blue'>This is a test</font>";
exit;
?>

EmpName 有 700 个条目,Project 有 70 个条目。 UsedArticle可以超过10k.

您的代码中有许多错误导致其无法正常工作。我已按如下方式对其进行了修改,并针对我的设置(Oracle 11gR2,PHP 5.6.x)成功地对其进行了测试。最重要的是别名 IN 值,这样它们就不会被周围的单引号引用 '.

<?php
$backcol = "bgcolor=#9fffa1"; // Light Green    
//$colgreen1 = "bgcolor=#5EE060"; // Light Green

//$maxitems = 1000; // Maximum items found to display

$s=" <small> ";

$user = 'odbc';
$pass = 'abcd';
$sid = 'server1.myComp.com';

//$adr = "http://server1/WebEditor";

$conn = oci_connect($user, $pass, $sid);
if (!$conn) {
    echo "Unable to connect: " .  var_dump(OCIError());
    die();
}

//}

$cmd = "SELECT *
FROM ( SELECT EmpName, UsedArticleNo, Project, EmpRol  FROM Emp_PRJ_Art)
PIVOT(COUNT(UsedArticleNo) FOR (Project) IN ('PRJ1' as prj1, 'PRJ2' as prj2, 'PRJ3' as prj3, 'PRJ4' as prj4))
ORDER BY EmpName";

$stid = oci_parse($conn,$cmd);
oci_execute($stid);

echo     "<table>
          <tr>
            <th $backcol> $s Employee Name </th>
            <th $backcol> $s Project-1 </th>
            <th $backcol> $s Project-2 </th>
            <th $backcol> $s Project-3 </th>
            <th $backcol> $s Project-4 </th>
            <th $backcol> $s EmpRol </th>
          </tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

    echo "<tr>\n";
    echo "<td> $s " . $row['EN'] . "</td>"
        ."<td> $s " . $row['PRJ1'] . "</td>"
        ."<td> $s " . $row['PRJ2'] . "</td>"
        ."<td> $s " . $row['PRJ3'] . "</td>"
        ."<td> $s " . $row['PRJ4'] . "</td>"
        ."<td> $s " . $row['ER'] . "</td>"
        ."</tr>"
    ;
}
echo "</table>\n";