我很难解释我的必要性,所以我会描述一下情况。
设想情况:
我们想要创建一个报告,在该报告中已经完成了计算,并且我们认为,根据这个场景的值,我们认为第一步是返回如下内容:
圆数量(千克)
1
2
第三章
第四章
5
/T1456-1996商业银行的商业、金融等行业的发展
7
8
9
在此之后,可以使用简单的操作来重新计算组件。
问题是,我们无法想出获得预期回报的方法,也想不出实现上述报告的另一种方式。
我们所能做的就是得到除法的整数部分
SELECT FLOOR(1027.5/125) AS "TEST" FROM DUMMY
剩下的
SELECT MOD(1027.5,125) AS "TEST" FROM DUMMY
我们正在使用:
如能提供任何帮助,将不胜感激。
提前感谢!
发布于 2020-05-29 07:32:28
有几种方法可以实现您所描述的希望。
一种方法是将需求转换为一个函数,该函数接受两个输入参数值并返回生产圆周表。
这个可以是这样的:
create or replace function production_rounds(
IN max_production_volume_per_round decimal (10, 2)
, IN production_order_volume decimal (10, 2)
)
returns table (
production_round integer
, production_volume decimal (10, 2))
as
begin
declare rounds_to_produce integer;
declare remainder_production_volume decimal (10, 2);
rounds_to_produce := floor( :production_order_volume / :max_production_volume_per_round);
remainder_production_volume := mod(:production_order_volume, :max_production_volume_per_round);
return
select /* generate rows for all "max" rounds */
s.element_number as production_round
, :max_production_volume_per_round as production_volume
from
series_generate_integer
(1, 1, :rounds_to_produce + 1) s
UNION ALL
select /* generate a row for the final row with the remainder */
:rounds_to_produce + 1 as production_round
, :remainder_production_volume as production_volume
from
dummy
where
:remainder_production_volume > 0.0;
end;
您可以像使用任何表一样使用这个函数,但是可以使用参数:
select * from production_rounds (125 , 1027.5) ;
PRODUCTION_ROUND PRODUCTION_VOLUME
1 125
2 125
3 125
4 125
5 125
6 125
7 125
8 125
9 27.5
可能需要解释的部分是SERIES_GENERATE_INTEGER
。这是一个特定于HANA的内置函数,它从一个“系列”中返回许多记录。这里的序列是一个在一个最小和最大限度内的周期序列,并且在两个相邻的周期之间有一定的步长。更多关于这一工作原理的信息可以在HANA参考文档中找到,但就目前而言,这是生成X行结果集的最快方法。
此系列生成器用于创建所有“完整”生产轮。然后,对于UNION ALL
的第二部分,通过从内置表DUMMY
(Oracle中的DUAL
)中选择只创建一行,保证只有一条记录。最后,如果实际上没有余数,则需要“禁用”第二部分,这是由WHERE
子句完成的。
https://stackoverflow.com/questions/62070513
复制相似问题