简体   繁体   English

获取最后交易日期的最后数据

[英]Get last data of last transaction date

I have a table for transactions that need to display the last transaction data row.我有一个需要显示最后一个交易数据行的交易表。 Here is the table:这是表:

Table Name : Product_detail_trx

Trans_date;  Prod_Code;  Outlet_code;   Qty;   Prev_bal_qty;
2020-02-26;  SKU1;       CNGI;          2;       1;
2020-02-20;  SKU1;       CNGI;         -3;       4;
2020-02-29;  SKU1;       CNGI;          5;       3;

I want to get the last trans_date with sum of qty and its prev_bal_qty .我想用 qty 及其prev_bal_qty总和获得最后一个prev_bal_qty

The data must be shown like this:数据必须像这样显示:

Trans_date Outlet_Code Qty_Total  Prev_bal_qty
2020-02-29; CNGI;        4;          3;

I tried to我试过了

select Max trans_date, sum (qty) as total_qty, prev_bal_qty
group by prod_code, outlet_code, prev_bal_qty

But it showed not in group.但它显示不在组中。

You can use window functions:您可以使用窗口函数:

select trans_date, outlet_code, qty_total, prev_bal_qty
from (
    select
        p.*,
        sum(qty) over() total_qty,
        row_number() over(order by trans_date desc) rn
    from product_detail_trx p
) p
where rn = 1

The inner query ranks records by descending date and computes the total quantity.内部查询按降序排列记录并计算总数。 The outer query filters on the top record.外部查询过滤顶部记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM