I am trying to determine the first entry for a product in SQL Server. The table is a log of JDE updates with a record status of A when it is added.
Our products are grouped where we have many codes for the same product with different batches. The first 19 chars of the product code will be the same regardless of batch.
Sample data:
Only the bolded row would be the record I want to return as that is the first entry for that First19 code.
This is the SQL I put together (Excuse the mess around the dates, it's what i have to do to make it a true date from how JDE stores dates):
SELECT DATEADD(DAY,CONVERT(INT,RIGHT(F4101Z1.SZUPMJ,3))-1,DATEADD(YEAR,CONVERT(INT,LEFT(F4101Z1.SZUPMJ,3)),'01/01/1900')) Modified_Date,
F4101Z1.SZTNAC Record_Status,
F4101Z1.SZLITM,
LEFT(F4101Z1.SZLITM,19) First19
FROM ODS.PRODDTA.F4101Z1 F4101Z1
LEFT OUTER JOIN (
SELECT LEFT(SZLITM,19) First19
FROM ODS.PRODDTA.F4101Z1
WHERE DATEADD(DAY,CONVERT(INT,RIGHT(SZUPMJ,3))-1,DATEADD(YEAR,CONVERT(INT,LEFT(SZUPMJ,3)),'01/01/1900')) = '11/12/2020'
) F4101Z1_2 ON LEFT(F4101Z1.SZLITM,19) = First19
WHERE F4101Z1_2.First19 IS NULL
AND F4101Z1.SZTNAC = 'A'
The code returns all 3 results which is not what I expect I expected the bolded entry only.
I actually want to put a date criteria on there so I can frame this into a report which I run for the previous day. Basically to show any NEW products that have been created where they are genuine new products and now new batches.