我对SQL还是很陌生,我希望从两个查询中获得一个AVG函数(按组加入一个查询)。
我的查询模板。
WITH group1 AS (
SELECT
ID AS ID
,date AS date
,TYPE AS TYPE
,GROUP_M AS GROUP_M
,Value AS Value
,AVG (NULLIF( Value , 0)) OVER (PARTITION BY TYPE ) AS TYPE_AVG
FROM
(
SELECT
a4.id AS ID
,a0.data AS date
,GET_ATTRIBUTE_VALUE ( a12.attributes , Upper('TYPE'), '=') AS TYPE
,GET_ATTRIBUTE_VALUE ( a12.attributes , Upper('GROUP_M'), '=') AS GROUP_M
,a4.value AS Value
,a12.attributes AS attributes
FROM
SESSION a2
LEFT JOIN P_id a0 ON a0.id = a2.id
INNER JOIN P_SET a3 ON a3.id = a2.id
WHERE
a2.name = 'REVIEW'
AND a2.test_name = '4M'
AND a0.data >= TRUNC(SYSDATE) - 7
)
),
group2 AS (
SELECT
,wch.id AS E_id
,e.entity AS Entity
FROM
F_machine wch
INNER JOIN F_Entity e ON e.entity = wch.entity
INNER JOIN F_Run_card lrc ON lrc.lotoperkey = wch.lotoperkey
INNER JOIN F_Route r ON r.route=lrc.route AND r.facility = lrc.facility AND r.latest_version = 'Y'
WHERE
e.entity Like 'E%'
AND e.entity Is Not Null
AND wch.end_time >= TRUNC(SYSDATE) - 14
)
SELECT *
FROM group1
JOIN group2 ON group1.id = group2.E_id
;
我的最终目标是再增加一列-机器和类型的平均价值
AVG (NULLIF( {value} , 0)) OVER (PARTITION BY {machine} , {TYPE} )
谢谢,希望一切都清楚了:)