从oracle数据库获取报告

时间:2015-02-06 11:37:42

标签: php oracle oci8

我正在研究一个从oracle数据库生成报告的php脚本。我想提高我得到的结果的质量。我希望表格的总数位于底部。 这就是我得到的。我希望十分位数要么是为了升序还是降序,我已经试图用sql来做(有一个我无法控制的十分位数10)。 我还希望将两列的总数(total_decile_count和FULL_KYC)放在那里。

ECILE   TOTAL_DECILE_COUNT  FULL_KYC    PERCENTAGE
Decile 9    5091             1936           38.03
Decile 8    12472           5580            44.74
Decile 7    29927           14838           49.58
Decile 6    36481           18770           51.45
Decile 5    33460           18356           54.86
Decile 4    30454           17010           55.85
Decile 3    24243           14175           58.47
Decile 2    16912           8245            48.75
Decile 10   4231            2122            50.15
Decile 1    8801            4835            54.94
Bal.barred  1188354        115601           9.73

这是我的代码。不介意sql,因为那里没有问题。我的兴趣是PHP代码。

<?php
ini_set('max_execution_time', 0);
ini_set('memory_limit', '1500M');
$c = oci_pconnect("xxx", "xxx", "xxxx");
if (!$c) {
$e = oci_error();
trigger_error('Could not connect to database: '. $e['message'],E_USER_ERROR);
}
$s = oci_parse($c, "WITH 
  dcl AS (
 select count(n.msisdn) FULL_KYC,case when n.decile_group is NULL then 'Bal.barred' else n.decile_group end decile_group  
from (select distinct (a.msisdn)msisdn,b.segment,b.decile_group 
from table1 a full join table2 b on a.msisdn=b.msisdn)n,
(select distinct msisdn from (
            select case
              when substr(msisdn,1,1) = '7' then ''||msisdn
              when substr(msisdn,1,1) = '0' then ''||substr(msisdn,2,9)
            else msisdn end msisdn from table3))p
where n.msisdn=p.msisdn
group by n.decile_group),

base as (select decile,total_decile_count from table4 )

select base.decile, base.total_decile_count,dcl.full_kyc,round(((dcl.full_kyc/base.total_decile_count)*100),2) Percentage
from dcl left join base on base.decile=dcl.decile_group order by base.decile desc");


if (!$s) {
$e = oci_error($c);
trigger_error('Could not parse statement: '. $e['message'], E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
$e = oci_error($s);
trigger_error('Could not execute statement: '. $e['message'], E_USER_ERROR);
}
echo "<table border='1'>\n";
$ncols = oci_num_fields($s);
echo "<tr>\n";
for ($i = 1; $i <= $ncols; ++$i) {
$colname = oci_field_name($s, $i);
echo " <th><b>".htmlentities($colname, ENT_QUOTES)."</b></th>\n";
}
echo "</tr>\n";
while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>".($item!==null?htmlentities($item,
ENT_QUOTES):"&nbsp;")."</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>

1 个答案:

答案 0 :(得分:0)

假设您的主要问题是正确的排序,您可以在sql中实现这一点,如下所示(仅order by部分很重要,其余部分用于演示):

with test_data as (
  select 'Decile 1' decile, 7 total_decile_count, 4 full_kyc, 38.00 Percentage from dual
  union select 'Decile 2', 12, 5, 45.00 from dual
  union select 'Decile 3', 11, 19, 42.00 from dual
  union select 'Decile 11', 11, 8, 8.87 from dual 
  )
select * 
  from (
    select decile, total_decile_count, full_kyc, percentage from test_data
    union
    select 'Sums' decile, sum(total_decile_count) total_decile_count, 
      sum(full_kyc) full_kyc, sum(percentage) percentage from test_data)
  order by case when decile like 'Decile%' then 1 else 2 end,
    length(trim(decile)) desc, decile desc

输出:

DECILE       TOTAL_DECILE_COUNT    FULL_KYC    PERCENTAGE
Decile 11                    11           8          8,87
Decile 3                     11          19         42,00
Decile 2                     12           5         45,00
Decile 1                      7           4         38,00
Sums                         41          36        133,87
相关问题