获取数据表中的百分比

时间:2015-09-15 12:19:50

标签: php sql oracle datatable

我正在使用datatable插件来显示SQL查询结果。我正在从我的查询计算百分比,但它给了我一个错误的值。有没有办法从datatable获得结果?

enter image description here

在图片中,我33是总(23+4+2+4)。我需要在值(23/33)*100旁边获得百分比23。这有可能吗?

SELECT AIP.TERMID,
       (select count(RAU.TERMNAME)
          FROM REPORT_API_USAGE RAU
          WHERE RAU.TERMID = AIP.TERMID AND
                RAU.VOCID = 4) as page_views,
       (select sum(count(RAU.TERMNAME))
          FROM REPORT_API_USAGE RAU
          WHERE RAU.VOCID = 4 and
                RAU.TERMID in (select AIP.TERMID
                                 from REPORT_API_PAGES AIP
                                 GROUP BY AIP.TERMID)
          group by RAU.VOCID) as page_views_sum,
       (select sum(count(AIP2.TERMID))
          from REPORT_API_PAGES AIP2
          join REPORT_API_USAGE RAU
            ON (RAU.TERMID = AIP2.TERMID AND
                RAU.VOCID = 4)
          where aip2.ctype = 'download'
          GROUP by AIP2.termid) as download_sum,
       (select sum(count(AIP2.TERMID))
          from REPORT_API_PAGES AIP2
          join REPORT_API_USAGE RAU
            ON (RAU.TERMID = AIP2.TERMID AND
                RAU.VOCID = 4)
          where aip2.ctype = 'event'
          GROUP by AIP2.termid) as event_sum,
       COUNT(case
               when aip.ctype = 'download' then 1
               else null
             end) as page_download,
       COUNT(case
               when aip.ctype = 'event' then 1
               else null
             end) as page_event
  from REPORT_API_PAGES AIP
  GROUP BY AIP.TERMID

download_sum& event_sum错了。

这是我如何显示结果

<div class="percent-container"> <div class="percent-count"> <div class="percent-count-value"> <?php print $RowT->page_event; ?> </div> <div class="percent-value"><?php print round(($RowT->page_event / $RowT->event_sum) * 100); ?>% </div> </div>
<div class="percent-item"> <div class="percent" style="width: <?php print round(($RowT->page_event / $RowT->event_sum) * 100); ?>%"> </div> </div> </div>

1 个答案:

答案 0 :(得分:0)

WITH data (value) AS (
    SELECT 23 FROM DUAL UNION ALL
    SELECT 4 FROM DUAL UNION ALL
    SELECT 2 FROM DUAL UNION ALL
    SELECT 4 FROM DUAL
)
SELECT
    VALUE, ROUND(RATIO_TO_REPORT(VALUE) OVER () * 100) PERCENTAGE
FROM
    data