分组汇总逆运算 - 如何将分组数据拆分为明细数据

【问题】

Good evening, take much of a help.
I need to create a SQL query for the following situation: I have a product, quantity, and a field called packaging. need my query to return the product and su The quantity repeatedly until the quantity in the "packaging". I do not know create this query, I thought about using the command "While", but do not know how.
Thank you.

The product is well in the products table.
cod_product, quantity, packing
123               , 40    , 2 
I need to create a query that shows this product as follows.
Sequential, product, quantity.
1                , 123      ,  20
2                , 123      ,  20
I need the result to be shown the way, because I need to print labels, so need a label to each package with its respective weight, I hope I have explained my need.

别人答:

WITH
  Pass0 as (select 1 as C union all select 1), --2 rows
  Pass1 as (select 1 as C from Pass0 as A, Pass0 as B),--4 rows
  Pass2 as (select 1 as C from Pass1 as A, Pass1 as B),--16 rows
  Pass3 as (select 1 as C from Pass2 as A, Pass2 as B),--256 rows
  Pass4 as (select 1 as C from Pass3 as A, Pass3 as B),--65536 rows
  Tally as (select row_number() over(order by C) as SEQUENCIAL from Pass4)
select 
T.SEQUENCIAL 
, P.IDPRD as COD_PRODUCT 
, P.QUANTIDADE / p.QTDEVOLUMEUNITARIO as QTD 
, P.IDMOV 
from TITMMOV P (NOLOCK)
inner join tally T (NOLOCK)
on P.QTDEVOLUMEUNITARIO >= T.SEQUENCIAL 
where P.IDMOV = 2431 
ORDER BY P.IDPRD , T.SEQUENCIAL 

【回答】

       将每条记录按规则变成多条,这相当于分组汇总的逆运算了。SQL可以实现这种算法,但要转换思路绕一大圈才行,代码复杂难懂。 如果数据量不是特别大,可以试试SPL,它能更简单方便地实现本算法,脚本如下:


A

1

=db.query(select * from products")

2

=A1.news(packing;   ~:sequential,A1.cod_product:product,A1.quantity/A1.packing:quantity)

A1sql取数

A2:创建由sequentialproductquantity构建的新序表,根据A1每条记录的packing值,将A1每条记录拆分成packing条记录插入新构建的序表中。

       集算器可以架在应用程序和数据库之间,向上层的java程序或报表工具提供JDBC接口,和使用数据库差不多,很简单。具体可参考Java 如何调用 SPL 脚本