SQL conundrum, how to select latest date for part, but only 1 row per part (unique)

前端 未结 3 448
面向向阳花
面向向阳花 2021-01-13 04:47

I am trying to wrap my head around this one this morning.

I am trying to show inventory status for parts (for our products) and this query only becomes

3条回答
  •  醉酒成梦
    2021-01-13 05:08

    EDIT: Be sure to test the performance of each solution. As pointed out in this question, the CTE method may outperform using ROW_NUMBER.

    ;with cteMaxDate as (
        select ldPart, max(ldDate) as MaxDate
            from inventoryReport
            group by ldPart
    )
    SELECT md.MaxDate, ir.ptProdLine, ir.inPart, ir.inSite, ir.inAbc, ir.ptUm, ir.inQtyOh + ir.inQtyNonet AS in_qty_oh, ir.inQtyAvail, ir.inQtyNonet, ir.ldCustConsignQty, ir.inSuppConsignQty
        FROM cteMaxDate md
            INNER JOIN inventoryReport ir
                on md.ldPart = ir.ldPart
                    and md.MaxDate = ir.ldDate
    

提交回复
热议问题