If I'm following what you want correctly, I think you can use OLAP. One of
the things OLAP can do is assign as row number to the output that will reset
to 1 after each group that you define.
In the below data, column 1 is the RRN, and the list is sorted by RRN, then
timestamp in descending order. So, records 1 & 2 should be the most recent
UB & UP for each RRN
WITH cte AS (
SELECT JOCTRR, JOTSTP, JOCODE, JOENTT,
ROW_NUMBER() OVER
(PARTITION BY JOCTRR ORDER BY JOCTRR, JOTSTP DESC)
AS RowNum
FROM @@DATA
WHERE JOENTT IN ('UP','UB'))
SELECT JOCTRR, JOTSTP, JOCODE, JOENTT,
CAST (ROWNUM AS INT) AS ROWNUM
FROM CTE
ORDER BY JOCTRR, ROWNUM
Rob
------------------------------
message: 4
date: Wed, 7 Sep 2022 10:58:28 -0400
from: "Art Tostaine, Jr." <atostaine@xxxxxxxxx>
subject: SQL to add records based on latest record id
I have a journal file extracted with EXPJRNE tool. I exported to a table
called JRNHDR.
I need to copy JRNHDR back to the production data for certain record id's.
The file is keyed by a sequential number. So I need to grab the last record
for each record id.
Typically there is a PT, UB, and UP for each record id, but there can be
multiple UB/UP pairs. I need the last one.
Is there a SQL statement that can grab the only the last record id? I have
date & time in the EXPJRNE created file but time isn't specific enough.
Thanks, Art
As an Amazon Associate we earn from qualifying purchases.
This thread ...
Re: RE: SQL to add records based on latest record id, (continued)
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.