我有一个查询,其中包含我想要显示的正确条件和字段:
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年所有数据的平均值,不包括异常值?即找到2013年的(tsp.end_date - tsp.start_date)
日期差异的平均值,其中大部分观察结果都是如此?
percentile_cont
功能会起作用吗?我不熟悉它,但我知道它计算特定列的百分位数。就我而言,我正在寻找(tsp.end_date - tsp.start_date)
之间的平均日期差异,但是大多数数据点的平均值(不包括异常值)。
非常感谢任何帮助。也许我正在以错误的方式处理这个查询。
答案 0 :(得分:3)
这样的事情会解决你的问题吗?
在内联视图中计算平均值和标准差,然后使用它 定义你的异常值。假设您认为异常值是平均值的两倍,那么:
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