2015-02-06 107 views
1

我正在研究一个从oracle数据库生成报告的php脚本。我想提高我得到的结果的质量。我希望桌子底部的总数。 这就是我所得到的。我希望十进制的顺序要么是正在进行的,要么是正在进行的,因为我已经用sql来做这件事了(我有一个十分之一的无法控制的东西)。 我也想在那里有两列(total_decile_count和FULL_KYC)的总数。从oracle数据库中获取报告

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

回答

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 
+0

非常感谢。我解决了订单问题。现在我唯一的问题是如何将这个结果通过电子邮件发送给excel文件。提前致谢。 – 2015-02-09 06:04:37

+0

很高兴我能帮到你。至于电子表格中的电子邮件结果,我认为您应该创建另一个问题,因为这是全新的主题,其他用户肯定会更有帮助。 – 2015-02-09 20:00:06