【问题标题】:Inventory Ageing Calculation view in Hana StudioHana Studio 中的库存账龄计算视图
【发布时间】:2018-01-16 21:00:48
【问题描述】:

我想在 hana studio 中制作一个计算视图,我必须在其中维护库存中待处理的产品的老化。

它将按照先到先得的原则减去...我的意思是,如果是产品 P001,那么 5500 将首先减去 1000,然后余额数量将减去 2000,依此类推,直到它为零。.. 下面是交易表..

Department  |Product  | Date               | Quantity  |Indicator
------------+---------+--------------------+-----------+---------
D001        |P001     | 01-Jul-2017        | 1000      |ADD
D001        |P001     | 10-Jul-2017        | 2000      |ADD
D001        |P001     | 15-Jul-2017        | 3000      |ADD
D001        |P001     | 16-Jul-2017        | 2000      |ADD
D001        |P001     | 18-Jul-2017        | 5500      |SUBTRACT
D001        |P002     | 12-Jul-2017        | 3000      |ADD
D001        |P002     | 14-Jul-2017        | 2000      |ADD
D001        |P002     | 15-Jul-2017        | 4000      |SUBTRACT

最终的输出应该是这样的。如果今天的日期是 2017 年 7 月 31 日

Department  |Product  | Date               | Quantity  |AgeinginDays
------------+---------+--------------------+-----------+---------
D001        |P001     | 15-Jul-2017        | 500       |16
D001        |P001     | 16-Jul-2017        | 2000      |15
D001        |P002     | 14-Jul-2017        | 1000      |17

【问题讨论】:

  • 有人吗?请帮忙

标签: hana hana-sql-script hana-studio


【解决方案1】:

我创建了 Inventory 表并用您的示例数据填充了它。 这是用于此的 SQL 代码。请在以后寻求帮助时尝试分享此类元数据:)

create column table InventoryItems
(
Department varchar(10),
Product varchar(10),
Date date,
Quantity int,
Indicator varchar(10)
);
insert into InventoryItems select 'D001','P001','20170701',1000,'ADD' from dummy;
insert into InventoryItems select 'D001','P001','20170710',2000,'ADD' from dummy;
insert into InventoryItems select 'D001','P001','20170715',3000,'ADD' from dummy;
insert into InventoryItems select 'D001','P001','20170716',2000,'ADD' from dummy;
insert into InventoryItems select 'D001','P001','20170718',5500,'SUBTRACT' from dummy;
insert into InventoryItems select 'D001','P002','20170712',3000,'ADD' from dummy;
insert into InventoryItems select 'D001','P002','20170714',2000,'ADD' from dummy;
insert into InventoryItems select 'D001','P002','20170715',4000,'SUBTRACT' from dummy;

示例表和数据准备好后,我准备了以下 SQLScript 代码进行老化。由于可能难以理解和遵循代码,因此我在stock aging SQL calculation on SAP HANA database分享了您的问题并提供了详细的解决方案

这里是帮助您满足要求的 SQL 代码

with Ins as (
    select 
        row_number() over (partition by Product order by date) as AddId,
        *,
        sum(Quantity) over (partition by Product order by Date rows unbounded preceding) as sumIn,
        sum(Quantity) over (partition by Product order by Date desc rows unbounded preceding) as sumX
    from InventoryItems 
    where 
        Indicator = 'ADD'
), Outs as (
    select 
        row_number() over (partition by Product order by date) as AddId,
        *,
        sum(Quantity) over (partition by Product order by Date rows unbounded preceding) as sumOut
    from InventoryItems 
    where 
        Indicator = 'SUBTRACT'
), Inv as (
    select
        ins.Product, max(sumIn) - max(ifnull(sumOut,0)) as inv
    from Ins
    left join Outs
        on ins.Product = outs.Product
    group by ins.Product
), calc as (
    select
        Ins.*,
        Inv.inv,
        inv-sumx as diff
    from Ins
    left join Inv 
        on ins.Product = inv.Product
)
select
    Department, Product, Date, 
    case when diff > 0 then quantity else quantity-abs(diff) end as Quantity,
    DAYS_BETWEEN(Date,current_date) as AgeingInDays
from (
    select *, 1 as rn from calc where diff >= 0
    union all
    (
        select * from (
            select 
                *, row_number() over (partition by Product order by diff desc) as rn 
            from calc 
            where diff < 0
        ) t where rn = 1
    )
)
order by Product, AddId

这是上面SQL块执行的输出

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多