[英]how to get only average values from joined table
I have the following query: 我有以下查询:
with D1 as (
select v06.ID
,AVG(TD.A) as A_avg
,AVG(TD.B) as B_avg
from v06
inner join v07 TD
on v06.ID= TD.ID
and v06.timestamp= TD.timestamp
where v06.X is not NULL
group by v06.ID
)
select * from D1
Now I'm grouping by v06.ID. 现在,我按v06.ID进行分组。 However, what I want is to keep all fields of v06 while only adding two averaged values from table TD.
但是,我想要保留v06的所有字段,而只从表TD中添加两个平均值。 So I tried:
所以我尝试了:
with D1 as (
select v06.*
,AVG(TD.A) as A_avg
,AVG(TD.B) as B_avg
from v06
inner join v07 TD
on v06.ID= TD.ID
and v06.timestamp= TD.timestamp
where v06.X is not NULL
group by v06.*
)
select * from D1
But that gives me the 'illegal symbol *' on the select statement. 但这在选择语句中给了我“非法符号*”。 Does anyone know how to achieve this?
有谁知道如何实现这一目标?
As one of the comment mention, you can't have a column in the select that isn't in the GROUP BY
clause or used in a aggregate function. 正如评论中提到的那样,您不能在select中有不在
GROUP BY
子句中或在聚合函数中使用的列。
Try this: 尝试这个:
with D1 as (
select v06.ID
,AVG(TD.A) as A_avg
,AVG(TD.B) as B_avg
from v06
inner join P.B90T75 TD
on v06.ID= TD.ID
and v06.timestamp= TD.timestamp
where v06.X is not NULL
group by v06.ID
)
select d2.*, d1.a_avg, d1.b_avg
from D1
join v06 D2
on d1.id = d2.id
Or, if available/supported [unknown if, per no mention of platform or release of DB2 in the OP, nor even a platform-specific DB2 tag included], perhaps the following query. 或者,如果可用/受支持[如果不提及OP中DB2的平台或发行版,甚至不包括特定于平台的DB2标记,则不知道],可能是以下查询。 Note: remove or adjust the DEC casting scalar as appropriate;
注意:适当地拆除或调整DEC浇铸标量; I just chose a small value to get a better report size with my sample data because there was no DDL with sample data nor expected result offered in the OP against which to test:
我只是选择一个较小的值,以使用示例数据获得更好的报告大小,因为在OP中没有要测试的DDL包含示例数据或预期结果:
select a.*
, dec( avg(a) over(partition by a.id ) , 11 ) as avg_a
, dec( avg(b) over(partition by a.id ) , 11 ) as avg_b
from v06 as a
join v07 as b
on a.id = b.id
and a.timestamp = b.timestamp
where x is not null
order by id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.