1

我有一个查询,其中包含我要显示的正确条件和字段:

  SELECT t.business_process_id,
         COUNT (tsp.status) AS COUNT,
         ROUND (AVG (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS average,
         ROUND (MAX (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS MAX,
         ROUND (MIN (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS MIN,
         ROUND (MEDIAN (tsp.end_date - tsp.start_date), 2) * 24 * 60 AS MEDIAN,
         ROUND (STDDEV (tsp.end_date - tsp.start_date), 2) AS std_deviation
    FROM transaction_status_period tsp, transaction t
   WHERE     t.trans_id = tsp.trans_id
         AND tsp.status = 'R'
         AND tsp.end_date IS NOT NULL
         AND tsp.userid NOT IN ('X', 'Y', 'Z', 'A')
         AND EXTRACT (DAY FROM tsp.start_date) =
                 EXTRACT (DAY FROM tsp.end_date)
         AND EXTRACT (YEAR FROM tsp.start_date) =
                 EXTRACT (YEAR FROM tsp.end_date)
         AND EXTRACT (MONTH FROM tsp.start_date) =
                 EXTRACT (MONTH FROM tsp.end_date)
         AND EXTRACT (YEAR FROM tsp.start_date) = 2013
         AND NOT EXISTS
                     (SELECT 1
                        FROM transaction_status_period tsp1
                       WHERE     tsp1.trans_id = tsp.trans_id
                             AND tsp.userid = tsp1.userid
                             AND tsp1.status = 'S'
                             AND tsp1.timestamp < tsp.timestamp)
GROUP BY t.business_process_id

查询计算的平均值是相关数据集的平均值(年份 = 2013)。有没有办法让查询计算 2013 年所有数据的平均值,不包括异常值?即找到(tsp.end_date - tsp.start_date)2013 年大多数观测值落在哪里的日期差异的平均值?

percentile_cont功能会起作用吗?我不熟悉它,但我知道它计算特定列的百分位数。就我而言,我正在寻找 之间的平均日期差异(tsp.end_date - tsp.start_date),但大多数数据点的平均值(不包括异常值)。

任何帮助将非常感激。也许我以错误的方式处理这个查询。

4

1 回答 1

5

这样的事情会解决你的问题吗?

在内联视图中计算您的平均偏差和标准偏差,然后使用它来定义您的异常值。假设您考虑的异常值是平均值标准的两倍:

SELECT calc.business_process_id,
 COUNT (calc.status) AS COUNT,
 ROUND (AVG (calc.end_date - calc.start_date), 2) * 24 * 60 AS average,
 ROUND (MAX (calc.end_date - calc.start_date), 2) * 24 * 60 AS MAX,
 ROUND (MIN (calc.end_date - calc.start_date), 2) * 24 * 60 AS MIN,
 ROUND (MEDIAN (calc.end_date - calc.start_date), 2) * 24 * 60 AS MEDIAN,
 ROUND (STDDEV (calc.end_date - calc.start_date), 2) AS std_deviation
FROM (SELECT t.business_process_id,
         tsp.status,
         tsp.start_date,
         tsp.end_date, 
         ntile(100) over (order by (tsp.end_date-tsp.start_date)) as percentiles
      FROM transaction_status_period tsp, transaction t 
      WHERE     t.trans_id = tsp.trans_id
      AND tsp.status = 'R'
      AND tsp.end_date IS NOT NULL
      AND tsp.userid NOT IN ('X', 'Y', 'Z', 'A')
      AND EXTRACT (DAY FROM tsp.start_date) =
          EXTRACT (DAY FROM tsp.end_date)
      AND EXTRACT (YEAR FROM tsp.start_date) =
          EXTRACT (YEAR FROM tsp.end_date)
      AND EXTRACT (MONTH FROM tsp.start_date) =
          EXTRACT (MONTH FROM tsp.end_date)
      AND EXTRACT (YEAR FROM tsp.start_date) = 2013
      AND NOT EXISTS
             (SELECT 1
                FROM transaction_status_period tsp1
               WHERE     tsp1.trans_id = tsp.trans_id
                     AND tsp.userid = tsp1.userid
                     AND tsp1.status = 'S'
                     AND tsp1.timestamp < tsp.timestamp)
  ) calc
WHERE calc.percentiles >=10 
AND calc.percentiles <=90
GROUP BY calc.business_process_id  
于 2014-06-06T03:46:11.977 回答