将整数转换为百分比

时间:2014-01-28 02:51:37

标签: oracle plsql oracle-sqldeveloper percentage plsqldeveloper

我不知道如何将整数转换为百分比,请帮帮我。谢谢

以下是查询:

SELECT 'Data' || ',' ||
       TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy') || ',' ||
       NVL(o.CNT_OPENED,0) || ',' || --as cnt_opened
       NVL(c.CNT_CLOSED,0)           --as cnt_closed
  FROM OWNER_DWH.DC_DATE d
  LEFT JOIN (SELECT TRUNC(t.CREATE_TIME, 'MM') AS report_date,
                    count(*) AS cnt_opened
               FROM APP_ACCOUNT.OTRS_TICKET t
               WHERE t.CREATE_TIME BETWEEN SYSDATE -120 AND SYSDATE
               GROUP BY TRUNC(t.CREATE_TIME, 'MM')) o
    ON d.DTIME_DAY=o.REPORT_DATE
  LEFT JOIN (SELECT TRUNC(t.CLOSE_TIME, 'MM') as report_date,
                    count(*) AS cnt_closed
               FROM APP_ACCOUNT.OTRS_TICKET t
               WHERE t.CLOSE_TIME BETWEEN SYSDATE -120 AND SYSDATE
               GROUP BY TRUNC(t.CLOSE_TIME, 'MM')) c
    ON D.DTIME_DAY=c.REPORT_DATE
  WHERE d.DTIME_DAY BETWEEN SYSDATE -120 AND TRUNC(SYSDATE) -1 AND
        d.DTIME_DAY = TRUNC(d.DTIME_DAY, 'MM') AND
        TRUNC(d.DTIME_DAY,'MM')= d.DTIME_DAY
  ORDER BY D.DTIME_DAY;

该查询的输出:

 Data,10/01/2013,219,201
 Data,11/01/2013,249,234
 Data,12/01/2013,228,224
 Data,01/01/2014,269,256

我需要的示例输出是这样的:

 Data,10/01/2013,219, 52%, 201, 45%
 Data,11/01/2013,249, 75%, 234, 60%
 .......
 ........

公式:

create_time + close time = total / create_time (for cnt_opened each column) = percentage
create_time + close time = total / close_time (for cnt_closed each column) = percentage

1 个答案:

答案 0 :(得分:2)

试试这个:

基本上只需添加CNT_OPENED和CNT_CLOSED的总和,然后根据您想要的百分比,将其乘以100并除以总和。

例如,CNT_OPENED = 219且CNT_CLOSED = 201,因此总数为420.将CNT_OPENED乘以100,然后除以420 - > (219 * 100)/ 420 = 52.使用CNT_CLOSED做同样的事情。

请注意,如果CNT_OPENED和CNT_CLOSED都为0,则会导致异常。

SELECT 'Data'
||','||TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy')
||','||NVL(o.CNT_OPENED,0) --as cnt_opened
||','||(NVL(o.CNT_OPENED,0) * 100) / (NVL(o.CNT_OPENED,0) + NVL(o.CNT_CLOSED,0)) || '%'
||','||NVL(c.CNT_CLOSED,0) --as cnt_closed
||','||(NVL(o.CNT_CLOSED,0) * 100) / (NVL(o.CNT_OPENED,0) + NVL(o.CNT_CLOSED,0)) || '%'

这也可能会给你一百万个小数位,所以如果你只想把它拿出来,只需使用TRUNC函数并指定你的精度(在这种情况下为2位小数):

TRUNC((NVL(o.CNT_OPENED,0) * 100) / (NVL(o.CNT_OPENED,0) + NVL(o.CNT_CLOSED,0)), 2)