ITPub博客

首页 > Linux操作系统 > Linux操作系统 > itpub论坛回复: 分组求积

itpub论坛回复: 分组求积

原创 Linux操作系统 作者:oldwain 时间:2019-06-22 09:09:07 0 删除 编辑

问题:

wenzai169:

从未遇到的SQL问题?

一表A结构大致如下
ID QTY
1 2
2 4
3 6
4 5
要求能够得到QTY字段的乘积2*4*6*5,

我的回答:


运用一下数学知识, 可以这样简化:

a * b * c = power(10, log(10, a) + log(10, b) + log(10, c)

这样, 楼主的问题可以用这样的单条sql来解决:

Select power(10, Sum(Log(10, qty))) From t

scott@O9I.US.ORACLE.COM> drop table t;

Table dropped.

scott@O9I.US.ORACLE.COM> create table t (id number, qty number);

Table created.

scott@O9I.US.ORACLE.COM> insert into t values(1, 2);

1 row created.

scott@O9I.US.ORACLE.COM> insert into t values(2, 4);

1 row created.

scott@O9I.US.ORACLE.COM> insert into t values(3, 6);

1 row created.

scott@O9I.US.ORACLE.COM> insert into t values(4, 5);

1 row created.

scott@O9I.US.ORACLE.COM> commit;

Commit complete.

scott@O9I.US.ORACLE.COM> Select power(10, Sum(Log(10, qty))) From t ;

POWER(10,SUM(LOG(10,QTY)))
--------------------------
240

scott@O9I.US.ORACLE.COM>

虽然举的例子没有用到分组功能, 不过加上group by,同样的方法依然可用。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/32/viewspace-184243/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 女儿让我感动
请登录后发表评论 登录
全部评论

注册时间:2001-09-24

  • 博文量
    49
  • 访问量
    39080