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

【问题】

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)

A1:sql取数

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值