gpt4 book ai didi

sql - Oracle表中分母最小公倍数的计算

转载 作者:行者123 更新时间:2023-12-04 00:53:39 27 4
gpt4 key购买 nike

我想创建一个表格来更好地说明我的问题

这是我在 Oracle 数据库中的数据示例

enter image description here

我想做的工作一步一步如下

1 - 按 group_id 列对分母列中的值进行分组并计算最小公倍数。对于此操作,我创建了 lcm(最小公倍数)和 gcd(最大公约数)函数。添加到这里。

CREATE OR REPLACE function SAMPLE.lcm(a number, b number)return number isbeginreturn (a*b)/gcd(a,b);end;

CREATE OR REPLACE function SAMPLE.gcd(a number, b number)
return number is
beginif b = 0 thenreturn a;elsereturn gcd(b,mod(a,b)); endif; end;

enter image description here

2-分子值与分母列的值成比例增加。数学公式如下:

(lcm(values of denominator(1..to -n)) / values of denominator ) *values of the numerator

enter image description here

3-通过按 group_id 值对新计算值进行分组来求和

enter image description here

所有能够完成这项工作的诸如sql、函数、 View 之类的项目都适合我。

我能为此做些什么。

enter image description here

最佳答案

[TL;DR] 编写您自己的自定义聚合函数。


鉴于您的 GCDLCM 函数:

CREATE FUNCTION GCD(
a IN NUMBER,
b IN NUMBER
) RETURN NUMBER DETERMINISTIC
IS
BEGIN
IF b = 0 THEN
RETURN a;
ELSE
RETURN GCD(b,MOD(a,b));
END IF;
END;
/

CREATE FUNCTION LCM(
a IN NUMBER,
b IN NUMBER
) RETURN NUMBER DETERMINISTIC
IS
BEGIN
RETURN (a*b)/GCD(a,b);
END;
/

然后您可以为自定义聚合中使用的类型创建规范:

CREATE TYPE LCMAggregationType AS OBJECT(
value NUMBER,

STATIC FUNCTION ODCIAggregateInitialize(
ctx IN OUT LCMAggregationType
) RETURN NUMBER,

MEMBER FUNCTION ODCIAggregateIterate(
self IN OUT LCMAggregationType,
value IN NUMBER
) RETURN NUMBER,

MEMBER FUNCTION ODCIAggregateTerminate(
self IN OUT LCMAggregationType,
returnValue OUT NUMBER,
flags IN NUMBER
) RETURN NUMBER,

MEMBER FUNCTION ODCIAggregateMerge(
self IN OUT LCMAggregationType,
ctx IN OUT LCMAggregationType
) RETURN NUMBER
);
/

与 body :

CREATE OR REPLACE TYPE BODY LCMAggregationType
IS
STATIC FUNCTION ODCIAggregateInitialize(
ctx IN OUT LCMAggregationType
) RETURN NUMBER
IS
BEGIN
ctx := LCMAggregationType( 1 );
RETURN ODCIConst.SUCCESS;
END;

MEMBER FUNCTION ODCIAggregateIterate(
self IN OUT LCMAggregationType,
value IN NUMBER
) RETURN NUMBER
IS
BEGIN
IF value IS NOT NULL THEN
self.value := LCM( self.value, value );
END IF;
RETURN ODCIConst.SUCCESS;
END;

MEMBER FUNCTION ODCIAggregateTerminate(
self IN OUT LCMAggregationType,
returnValue OUT NUMBER,
flags IN NUMBER
) RETURN NUMBER
IS
BEGIN
returnValue := self.value;
RETURN ODCIConst.SUCCESS;
END;

MEMBER FUNCTION ODCIAggregateMerge(
self IN OUT LCMAggregationType,
ctx IN OUT LCMAggregationType
) RETURN NUMBER
IS
BEGIN
self.value := LCM( self.value, ctx.value );
RETURN ODCIConst.SUCCESS;
END;
END;
/

然后您可以创建自定义聚合函数:

CREATE FUNCTION LCM_AGG( value NUMBER )
RETURN NUMBER
PARALLEL_ENABLE AGGREGATE USING LCMAggregationType;
/

如果您有示例数据:

CREATE TABLE table_name ( id, group_id, numerator, denominator ) AS
SELECT 1, 13, 4, 12 FROM DUAL UNION ALL
SELECT 2, 13, 33, 126 FROM DUAL UNION ALL
SELECT 3, 13, 8, 45 FROM DUAL UNION ALL
SELECT 4, 28, 56, 137 FROM DUAL UNION ALL
SELECT 5, 28, 13, 236 FROM DUAL UNION ALL
SELECT 6, 28, 69, 145 FROM DUAL;

您可以使用查询:

SELECT group_id,
SUM( numerator ) AS numerator,
MAX( denominator ) AS denominator
FROM (
SELECT group_id,
numerator * LCM_AGG( denominator ) OVER ( PARTITION BY group_id ) / denominator
AS numerator,
LCM_AGG( denominator ) OVER ( PARTITION BY group_id ) as denominator
FROM table_name
)
GROUP BY group_id;

哪些输出:

GROUP_ID | NUMERATOR | DENOMINATOR-------: | --------: | ----------:      13 |       974 |        1260      28 |   4405473 |     4688140

db<> fiddle here

关于sql - Oracle表中分母最小公倍数的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64396885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com