【问题标题】: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

【问题讨论】:

    标签: sql group-by


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 2017-09-25
      • 1970-01-01
      相关资源
      最近更新 更多