gpt4 book ai didi

sql - 如何从 PostgreSQL 的表中找到值子集的平均值?

转载 作者:行者123 更新时间:2023-12-03 19:39:50 26 4
gpt4 key购买 nike

请原谅我,但我是 PostgreSQL 的新手,并且负责更新某些表中的某些字段。一个特定字段是平均决策时间,如下所示:

CASE WHEN COUNT(tdrm.dbid) > 0
THEN TO_CHAR((AVG(tdrm.total_processing_time) || ' millisecond')::interval, 'MI:SS.MS')
ELSE '00:00.000'
END AS average_decision_time
哪里 COUNT(tdrm.dbid)items_seen .此逻辑的问题在于,我们希望从具有等于“AF_ABORT”的中止标志的项目的平均值中排除总处理时间。
这就是我想要做的:
CASE WHEN COUNT(tdrm.dbid) > 0
THEN TO_CHAR((AVG(COUNT(CASE WHEN tdrm.tdr_abort_flag!=AF_ABORT THEN tdrm.total_processing_time END)) || ' millisecond')::interval, 'MI:SS.MS')
ELSE '00:00.000'
END AS average_decision_time
但我收到以下错误:

ERROR: aggregate function calls cannot be nestedLINE 64: THEN TO_CHAR((AVG(COUNT(CASE WHEN tdrm.tdr_abort_flag!=A...


我是在正确的轨道上还是有更简单的方法来做到这一点?
完整的 SQL 如下:
SELECT s.*,
CASE WHEN agent_event.event_code = 'data_download' THEN 'DL'
WHEN agent_event.event_code = 'mode' THEN 'Mode'
ELSE agent_event.event_code
END AS userAction
FROM
(
WITH report_constants AS (
-- Decisions from DetectionReport.h
SELECT
0::int as AD_UNKNOWN,
1::int as AD_ALARM,
2::int as AD_CLEAR,
-- Flags from DetectionReport.h
0::int as AF_UNKNOWN,
1::int as AF_ABORT,
2::int as AF_SUCCESS,
-- UI values for Decisions are DIFFERENT
0::int as UI_AD_ALL,
1::int as UI_AD_CLEAR,
2::int as UI_AD_ALARM,
3::int as UI_AD_UNKNOWN,
--
0::int as AGENT_TYPE_SCANNER,
1::int as AGENT_TYPE_OSR,
2::int as AGENT_TYPE_DIVERTER,
3::int as AGENT_TYPE_TIP,
4::int as AGENT_TYPE_SEARCH,
-- Operation Mode from Module.h
0::int as OPERATION_MODE_UNKNOWN,
1::int as OPERATION_MODE_SCAN,
2::int as OPERATION_MODE_OTHER
)
SELECT
nss_user.username AS user_name,
reg_login.action_time AS login_action_time,
reg_logout.action_time AS logout_action_time,
to_char(reg_login.action_time, 'MM-DD-YYYY') AS login_date,
to_char(reg_login.action_time, 'HH24:MI:SS') AS login_time,
CASE WHEN reg_logout.action_time IS NULL THEN '' ELSE
to_char(reg_logout.action_time, 'MM-DD-YYYY') END AS logout_date,
CASE WHEN reg_logout.action_time IS NULL THEN '' ELSE
to_char(reg_logout.action_time, 'HH24:MI:SS') END AS logout_time,
CASE WHEN user_level.name LIKE 'Level %' THEN SUBSTRING(user_level.name from 7) ELSE user_level.name END AS userAccess,
COUNT(tdrm.dbid) AS items_seen,
CASE WHEN COUNT(tdrm.dbid) > 0
THEN ROUND(100.0 * COUNT(CASE WHEN tdrm.tdr_abort_flag=AF_SUCCESS
AND tdrm.tdr_alarm_decision=AD_CLEAR THEN 1 END) / COUNT(tdrm.dbid), 2)
ELSE 0.00
END AS clear_rate,
COUNT(CASE WHEN (tdrm.tdr_abort_flag=AF_SUCCESS
AND tdrm.tdr_alarm_decision=AD_UNKNOWN)
OR tdrm.tdr_abort_flag=AF_ABORT THEN 1 END) AS operator_timeout,
CASE WHEN COUNT(tdrm.dbid) > 0
THEN ROUND(100.0 * COUNT(CASE WHEN tdrm.tdr_abort_flag=AF_SUCCESS
AND tdrm.tdr_alarm_decision=AD_ALARM THEN 1 END) / COUNT(tdrm.dbid), 2)
ELSE 0.00
END AS suspect_rate,
CASE WHEN COUNT(tdrm.dbid) > 0
THEN ROUND(100.0 * COUNT(CASE WHEN
(tdrm.tdr_abort_flag=AF_SUCCESS AND tdrm.tdr_alarm_decision=AD_UNKNOWN)
OR tdrm.tdr_abort_flag=AF_ABORT THEN 1 END) / COUNT(tdrm.dbid), 2)
ELSE 0.00
END AS operatorNoDecisionRate,
CASE WHEN COUNT(tdrm.dbid) > 0
THEN TO_CHAR((AVG(CASE WHEN tdrm.tdr_abort_flag!=AF_ABORT THEN (tdrm.total_processing_time) END) || ' millisecond')::interval, 'MI:SS.MS')
ELSE '00:00.000'
END AS average_decision_time
v2_module.dbid AS v2_gem_dbid
FROM report_constants CROSS JOIN auth_event
INNER JOIN registration_event AS reg_login
ON reg_login.credential_id=auth_event.credential_id
AND reg_login.event_type=3
LEFT OUTER JOIN registration_event AS reg_logout
ON reg_logout.credential_id=auth_event.credential_id
AND reg_logout.event_type=4
INNER JOIN nss_user ON nss_user.dbid=auth_event.nss_user_dbid
INNER JOIN user_level ON user_level.dbid=nss_user.user_level_dbid
LEFT OUTER JOIN bag_tdr ON nss_user.dbid=bag_tdr.author_user_dbid
AND (item_tdr.agent_type=AGENT_TYPE_OSR OR
item_tdr.agent_type=AGENT_TYPE_SEARCH)
AND item_tdr.author_credential_id=auth_event.credential_id
LEFT OUTER JOIN v2_module AS tdrm ON
item_tdr.v2_module_dbid=tdrm.dbid
LEFT OUTER JOIN v2_general_equipment_module
ON v2_general_equipment_module.dbid=reg_login.v2_gem_dbid
WHERE auth_event.credential_id IS NOT NULL
AND auth_event.auth_event_type=1
AND ($P{userid} = 'ALL' OR $P{userid} = nss_user.username)
AND item_tdr.created_date >= $P{fromdate}
AND item_tdr.created_date <= $P{todate}
AND v2_module.operation_mode != OPERATION_MODE_OTHER
GROUP BY nss_user.username, user_level.name, reg_login.agent_type,
reg_login.action_time, reg_logout.action_time,
v2_module.dbid
) s
LEFT OUTER JOIN agent_event
ON s.v2_dbid=agent_event.v2_dbid
AND agent_event.event_timestamp >= s.login_action_time
AND (s.logout_action_time IS NULL OR agent_event.event_timestamp <= s.logout_action_time)
ORDER BY s.login_action_time

最佳答案

we want to exclude the total processing time from the average for items that have an abort flag equal to 'AF_ABORT'.


CASE WHEN count(tdrm.dbid) > 0
THEN to_char(avg(tdrm.total_processing_time)
FILTER (WHERE tdrm.tdr_abort_flag IS DISTINCT FROM 'AF_ABORT') -- ①, ②
* interval '1 millisecond' -- ③
, 'MI:SS.MS')
ELSE '00:00.000'
END AS average_decision_time
① 实现过滤器的关键元素是聚合 FILTER条款。看:
  • Conditional SQL count

  • ② 如 tdrm.tdr_abort_flag可以为 NULL(缺少信息),我们需要 tdrm.tdr_abort_flag IS DISTINCT FROM 'AF_ABORT' .否则我们可以简化为 tdrm.tdr_abort_flag <> 'AF_ABORT' .
  • The manual about "Comparison Predicates"

  • ③ 乘法比串联加转换快得多。
    但是添加 FILTER 后像这样,表达式毕竟可以产生 NULL 值。你的要求很模糊。你可能真的想要: total_processing_time的平均值哪里 tdr_abort_flag <> 'AF_ABORT' .默认为 0如果结果是 NULL出于任何原因:
    COALESCE(to_char(avg(tdrm.total_processing_time) FILTER (WHERE tdrm.tdr_abort_flag <> 'AF_ABORT')
    * interval '1 millisecond'
    , 'MI:SS.MS')
    , '00:00.000') AS average_decision_time
    或者: total_processing_time的平均值哪里 tdr_abort_flag <> 'AF_ABORT' .但前提是 count(tdrm.dbid) > 0 .默认为 0如果由于任何原因结果为 NULL:
    CASE WHEN count(tdrm.dbid) > 0
    THEN COALESCE(to_char(avg(tdrm.total_processing_time) FILTER (WHERE tdrm.tdr_abort_flag <> 'AF_ABORT')
    * interval '1 millisecond'
    , 'MI:SS.MS')
    , '00:00.000')
    ELSE '00:00.000'
    END AS average_decision_time
    或者: total_processing_time的平均值哪里 tdr_abort_flag <> 'AF_ABORT' .但前提是 count(tdrm.dbid) > 0哪里 tdr_abort_flag <> 'AF_ABORT' .否则默认为 0
    CASE WHEN count(tdrm.dbid) FILTER (WHERE tdrm.tdr_abort_flag <> 'AF_ABORT') > 0
    THEN to_char(avg(tdrm.total_processing_time) FILTER (WHERE tdrm.tdr_abort_flag <> 'AF_ABORT')
    * interval '1 millisecond'
    , 'MI:SS.MS')
    ELSE '00:00.000'
    END AS average_decision_time
    您提到您是“SQL 新手”。让我补充一点:问题的清晰定义是 > 50 % 的解决方案。在许多领域都是如此,但对于 SQL 肯定如此。

    关于sql - 如何从 PostgreSQL 的表中找到值子集的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66495697/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com