简体   繁体   English

DB2查询以获取最新数据

[英]DB2 Query to get Latest Data

I need to query a DB2 table to get the latest data stored in the table by each unique Sup_Num and CONTAINER_CODE combination. 我需要查询DB2表,以通过每个唯一的Sup_Num和CONTAINER_CODE组合来获取存储在表中的最新数据。 The problem I'm having is that I can't figure out how to get only the data with the most recent date (INSERTED_DT). 我遇到的问题是我无法弄清楚如何仅获取最近日期(INSERTED_DT)的数据。 I tried using the Max() function on the INSERTED_DT field, but it gave me the exact same result I get without using the Max() function. 我尝试在INSERTED_DT字段上使用Max()函数,但是它给了我完全相同的结果,而无需使用Max()函数。 I didn't see anything like this when I searched for Max() function usage examples so I'm hoping someone on here could help. 当我搜索Max()函数用法示例时,我没有看到任何类似的东西,所以我希望这里的人能对您有所帮助。

The query I am using is this: 我正在使用的查询是这样的:

Select Distinct
    SupLookup.SUPPLIER_NO concat SupLookup.SUPPLIER_LOCATION AS Sup_Num,
    SupLookup.CONTAINER_CODE,
    AllocationType.ALLOC_TYPE_DESC,
    Allocation.ALLOC_QTY,
    Allocation.SAFE_STOCK_QTY,
    Allocation.ALLOC_QTY + Allocation.SAFE_STOCK_QTY AS CALC_TOT_ALLOC_REQ_QTY,
    SupLookup.ALLOC_REQ_QTY,
    SupLookup.TOT_ALLOC_REQ_QTY,
    SupLookup.DISC_QTY,
    SupLookup.FILL_PERCENT,
    MAX(SupLookup.INSERTED_DT) as Insert_DT

From RCX.RXSAL1 Allocation
    Inner Join rcx.RXALT1 AllocationType on Allocation.ALLOC_TYPE_ID = AllocationType.ALLOC_TYPE_ID
    Left Join rcx.RXPIR1 SupLookup on Allocation.SUPPLIER_ID = SupLookup.SUPPLIER_ID And allocation.CONTAINER_TYPE_ID = SupLookup.CONTAINER_TYPE_ID

Where Allocation.PLANT_ID= '50000036'

Group by 
    SupLookup.SUPPLIER_NO concat SupLookup.SUPPLIER_LOCATION,
    SupLookup.CONTAINER_CODE,
    AllocationType.ALLOC_TYPE_DESC,
    Allocation.ALLOC_QTY,
    Allocation.SAFE_STOCK_QTY,
    Allocation.ALLOC_QTY + Allocation.SAFE_STOCK_QTY,
    SupLookup.ALLOC_REQ_QTY,
    SupLookup.TOT_ALLOC_REQ_QTY,
    SupLookup.DISC_QTY,
    SupLookup.FILL_PERCENT

ORDER BY Sup_Num ASC

What I'm getting looks like this: 我得到的是这样的:

不良数据

But what I want is for it to pick out just the most recent date for each combination of Sup_Num and Container_Code like this: 但是我想要的是为Sup_Num和Container_Code的每种组合只选择最近的日期,如下所示:

好资料

-EDIT- -编辑-

The simple max/group by sub query here fails in this instance (server timeout). 在此情况下, 此处简单的max / group by子查询在此情况下失败(服务器超时)。 Someone suggested it at one point but has since removed the post. 有人建议这样做,但此后删除了该帖子。

Your query does not work because FILL_PERCENT is more less unique making up a separate group for each row. 您的查询不起作用,因为FILL_PERCENT的唯一性更差,因此每行组成一个单独的组。 Alternatively to the subquery mentioned by scaisEdge you could also use the ROW_UMBER function - order by INSERTED_DT descending and only retrieve the first row of each SupLookup.SUPPLIER_NO, SupLookup.CONTAINER_CODE combination. 除了scaisEdge提到的子查询之外,您还可以使用ROW_UMBER函数-以INSERTED_DT降序排列,并且仅检索每个SupLookup.SUPPLIER_NO,SupLookup.CONTAINER_CODE组合的第一行。

This would look like this: 看起来像这样:

with temp as (
    Select 
        SupLookup.SUPPLIER_NO concat SupLookup.SUPPLIER_LOCATION AS Sup_Num,
        SupLookup.CONTAINER_CODE,
        AllocationType.ALLOC_TYPE_DESC,
        Allocation.ALLOC_QTY,
        Allocation.SAFE_STOCK_QTY,
        Allocation.ALLOC_QTY + Allocation.SAFE_STOCK_QTY AS CALC_TOT_ALLOC_REQ_QTY,
        SupLookup.ALLOC_REQ_QTY,
        SupLookup.TOT_ALLOC_REQ_QTY,
        SupLookup.DISC_QTY,
        SupLookup.FILL_PERCENT,
        SupLookup.INSERTED_DT,
        row_number() over (partition by SupLookup.SUPPLIER_NO, SupLookup.CONTAINER_CODE order by SupLookup.INSERTED_DT desc) as rownum

    From RCX.RXSAL1 Allocation
        Inner Join rcx.RXALT1 AllocationType on Allocation.ALLOC_TYPE_ID = AllocationType.ALLOC_TYPE_ID
        Left Join rcx.RXPIR1 SupLookup on Allocation.SUPPLIER_ID = SupLookup.SUPPLIER_ID And allocation.CONTAINER_TYPE_ID = SupLookup.CONTAINER_TYPE_ID

    Where Allocation.PLANT_ID= '50000036'
) 
SELECT Sup_Num, CONTAINER_CODE, ALLOC_TYPE_DESC, ALLOC_QTY, SAFE_STOCK_QTY, CALC_TOT_ALLOC_REQ_QTY, ALLOC_REQ_QTY, TOT_ALLOC_REQ_QTY, DISC_QTY, FILL_PERCENT,INSERTED_DT
  FROM temp
WHERE rownum = 1

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

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