Skip to content
Advertisement

How to show the closest date to the selected one

I’m trying to extract the stock in an specific date. To do so, I’m doing a cumulative of stock movements by date, product and warehouse.

Without the and (select max(m2.fecdoc) from MOVSTOCKS m2) < '2020-11-30T00:00:00.000' it shows data like this, which is ok.

The problem is, with the subselect in the where clause it returns no results (I think it is because it just looks for the max date and says it is bigger than 2020-11-30). I would like it to show the closest dates (all of them, for each product and warehouse) to the selected one, in this case 2020-11-30.

It should look slike this:

Sorry if I’m not clear. Ask me if I have to clarify anything

Thank you

Advertisement

Answer

I am guessing that you want something like this:

The subquery calculates the cumulative amount of stock using window functions and filters for records before the cutoff date. The outer query selects the most recent record from the combination of codeart/codalm, which seems to be how you are identifying a product.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement