如何获得oracle中的百分比?

时间:2014-02-07 08:18:26

标签: sql oracle percentage

我的查询出现问题。无论我如何尝试,我都无法获得百分比。请帮我。我需要在单个查询中每周获得POS和NON-POS的百分比创建和关闭时间。我总是得到一个错误。谢谢!

查询:

对于POS:

SELECT 'Data'
||','||COUNT(CASE WHEN UPPER(TICKET_CUSTOMER_USER_ID) = UPPER('POS-generic') THEN 1 ELSE     NULL END) --POS
FROM APP_ACCOUNT.OTRS_TICKET
WHERE TRUNC(CREATE_TIME, 'IW') BETWEEN SYSDATE -119 AND SYSDATE -1
AND trunc(CLOSE_TIME, 'IW')  BETWEEN SYSDATE -119 AND SYSDATE -1;

对于非POS

  SELECT 'Data'
||','||COUNT(CASE WHEN UPPER(TICKET_CUSTOMER_USER_ID) = UPPER('hcphuser') THEN 1 ELSE NULL END) --NON_POS
FROM APP_ACCOUNT.OTRS_TICKET
WHERE TRUNC(CREATE_TIME, 'IW') BETWEEN SYSDATE -119 AND SYSDATE -1
AND trunc(CLOSE_TIME, 'IW')  BETWEEN SYSDATE -119 AND SYSDATE -1;

数据库的一些输出:

Name                           ticket_customer_id create_time   close_time
--------------------------------------------------------------------------
Cannot access the Zimbra Email POS-generic        10/15/2013    10/15/2013 
Slow WIFI Connection           POS-generic        10/15/2013    10/16/2013
Change phone number            hcphuser           10/17/2013    10/18/2013  

输出样本需求:

FOR POS

 Data, 10/10/2013, 50, 60%
 Data, 10/17/2013, 30, 40%

FOR NON POS

 Data, 10/10/2013, 70, 60%
 Data, 10/17/2013, 50, 40%

1 个答案:

答案 0 :(得分:1)

我认为你需要内部选择..试试这个

SELECT 'Data', POS, NON_POS, ROUND((POS*100 / (NON_POS + POS)),2) NON_POS_PERCENTAGE, 100 - ROUND((POS*100 / (NON_POS + POS)),2) POS_PERCENTAGE 
  FROM (
        SELECT 70 POS,
               30 NON_POS
        FROM DUAL
        )

         SELECT 'Data', POS, NON_POS, ROUND((POS*100 / (NON_POS + POS)),2) NON_POS_PERCENTAGE, 100 - ROUND((POS*100 / (NON_POS + POS)),2) POS_PERCENTAGE 
      FROM (
            SELECT COUNT(CASE WHEN UPPER(TICKET_CUSTOMER_USER_ID) = UPPER('POS-generic') THEN 1 ELSE     NULL END) POS,
                   COUNT(CASE WHEN UPPER(TICKET_CUSTOMER_USER_ID) = UPPER('hcphuser') THEN 1 ELSE NULL END) NON_POS
            FROM APP_ACCOUNT.OTRS_TICKET
            WHERE TRUNC(CREATE_TIME, 'IW') BETWEEN SYSDATE -119 AND SYSDATE -1
            AND trunc(CLOSE_TIME, 'IW')  BETWEEN SYSDATE -119 AND SYSDATE -1
            )