如何通过以下查询使用按功能分组?
Posted
技术标签:
【中文标题】如何通过以下查询使用按功能分组?【英文标题】:How can I use group by function with below query? 【发布时间】:2014-05-08 13:44:38 【问题描述】:如何通过以下查询使用分组功能?
实际上我尝试加入(内连接)两个包含 2 个字段的表。
没关系:
t1.fiels6 = t2.field7
这在内部连接函数中不起作用。因此我在 where 函数中使用了它:
t1.field8 = SUBSTRING(t2.field9,1,INSTR(t2.field9,'-') - 1)
and (INSTR(t2.field9,'-') - 1) < 14
and (INSTR(t2.field9,'-') - 1) > 0
我的主要目标是获取每组 t2.field2 的 avg((t1.field3 * t2.field4)) 和 stddev((t1.field3 * t2.field4))
select
t1.field1 as f1, t2.field2 as f2,(t1.field3 * t2.field4) as f5
from
pub.table1 t1, pub.table2 t2
where
t1.fiels6 = t2.field7
and t1.field6 = 'XXX'
and t1.field8 = SUBSTRING(t2.field9,1,INSTR(t2.field9,'-') - 1)
and (INSTR(t2.field9,'-') - 1) < 14 and (INSTR(t2.field9,'-') - 1) > 0
【问题讨论】:
【参考方案1】:将你拥有的内容移动到内部选择和分组 --
select f2, avg(calced), atddev(calced)
from
(
select
t1.field1 as f1,
t2.field2 as f2,
(t1.field3 * t2.field4) as calced
from pub.table1 t1
join pub.table2 t2 on t1.fiels6 = t2.field7
and (INSTR(t2.field9,'-') - 1) < 14
and (INSTR(t2.field9,'-') - 1) > 0
and t1.field8 = SUBSTRING(t2.field9,1,INSTR(t2.field9,'-') - 1)
where t1.field6 = 'XXX'
) t
group by f2
【讨论】:
以上是关于如何通过以下查询使用按功能分组?的主要内容,如果未能解决你的问题,请参考以下文章
WP Elementor:按日期过滤存档帖子(例如通过自定义查询)