2015-02-06 158 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