Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

SQL max query

$
0
0

Hello guys i am trying to make a query but i cannot manage to do nothing, its just frustating.

I have a table which contains entries and outs from the warehouse by year and month.

I put below a screenshot with the important fields and what are each one :

ITMREF : Item code

STOFCY : Factory , must be BZSC

PERSTR : Start period

PEREND: End period

FIYNUM : Year

PERNUM: Month

YEARCPQTY: year entries

YEAISSQTY : year outs

I manage to get the max value by using this query :
SELECT ITMREF_0, STOFCY_0, FIYNUM_0, PERNUM_0, YEARCPQTY_0, YEAISSQTY_0
FROM (SELECT t .*, max(PERSTR_0) OVER (partition BY ITMREF_0) AS last_period_start
FROM SCHEMA.ITMMVTHIS t) q
WHERE PERSTR_0 = last_period_start

The problem is that query returns me the MAX value , but some items have no movement this year and gives me the MAX which is last year last...


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>