I think this might be a cleaner solution:
with DATE_ORDERED as (
select row_number() over(
partition by PRODUCT#,
date(TIME_STAMP)
order by TIME_STAMP desc
) as SEQ,
X.*
from PRODFA.AVLAUDP X
)
select * from DATE_ORDERED where SEQ = 1 order by PRODUCT, TIME_STAMP
Tim.
________________________________
From: MIDRANGE-L <midrange-l-bounces@xxxxxxxxxxxxxxxxxx> on behalf of Alan Shore via MIDRANGE-L <midrange-l@xxxxxxxxxxxxxxxxxx>
Sent: 02 December 2020 20:16
To: Therrien, Paul <ptherrien@xxxxxxxxxxx>; Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxxxxxxxx>
Cc: Alan Shore <ashore@xxxxxxxx>
Subject: RE: Obtaining the last record for the day
Thanks Paul
This is what I ended up with
with itemsavail as
(SELECT
PRODUCT#, AVAIL_QTY, TIME_STAMP, date(TIME_STAMP) as AADATE
FROM PRODFA.avlaudp
WHERE date(TIME_STAMP) between
'2020-11-06' and '2020-11-29'
order by date(TIME_STAMP), PRODUCT#),
itemshighest as
(select z.PRODUCT#, z.AADATE, max(z.TIME_STAMP) as Hights
from itemsavail z
group by z.PRODUCT#, z.AADATE
order by z.PRODUCT#, z.AADATE)
select a.PRODUCT#, a.AADATE, a.Hights, b.AVAIL_QTY
FROM itemshighest A LEFT join itemsavail b on
a.PRODUCT# = b.PRODUCT# and
a.AADATE = b.AADATE and
a.Hights = b.TIME_STAMP
order by a.AADATE, a.PRODUCT#
Alan Shore
E-mail : ASHORE@xxxxxxxx
Phone [O] : (631) 200-5019
Phone [C] : (631) 880-8640
'If you're going through hell, keep going.'
Winston Churchill
-----Original Message-----
From: Therrien, Paul [mailto:ptherrien@xxxxxxxxxxx]
Sent: Wednesday, December 2, 2020 2:08 PM
To: Alan Shore <ashore@xxxxxxxx>; Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxxxxxxxx>
Subject: [EXTERNAL] RE: Obtaining the last record for the day
where date(time_stamp) = some_date
-----Original Message-----
From: Alan Shore [mailto:ashore@xxxxxxxx]
Sent: Wednesday, December 2, 2020 2:03 PM
To: Therrien, Paul <ptherrien@xxxxxxxxxxx>; Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxxxxxxxx>
Subject: RE: Obtaining the last record for the day
Thanks for your reply Paul
I had an epiphany just after I sent the e-mail and I am heading down this same direction The one thing missing from your example is the highest timestamp for the day, not just the highest timestamp
Alan Shore
E-mail : ASHORE@xxxxxxxx
Phone [O] : (631) 200-5019
Phone [C] : (631) 880-8640
'If you're going through hell, keep going.'
Winston Churchill
-----Original Message-----
From: Therrien, Paul [mailto:ptherrien@xxxxxxxxxxx]
Sent: Wednesday, December 2, 2020 1:57 PM
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxxxxxxxx>
Cc: Alan Shore <ashore@xxxxxxxx>
Subject: [EXTERNAL] RE: Obtaining the last record for the day
something like this:
select product, available_qty, time_stamp from myfile a where (product, time_stamp) in ( select product, max(time_stamp) from myfile b where b.product = a.product group by product);
-----Original Message-----
From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxxxxxxxx] On Behalf Of Alan Shore via MIDRANGE-L
Sent: Wednesday, December 2, 2020 1:12 PM
To: midrange-l@xxxxxxxxxxxxxxxxxx
Cc: Alan Shore <ashore@xxxxxxxx>
Subject: Obtaining the last record for the day
Hi everyone
We are on V7r3
We have a file that is an audit of the available quantity of products The fields are Product, time_stamp, Available_Qty I need to obtain the LAST record for a product within the same day For example Product time_stamp Available_Qty
5
2020-11-06-19.19.33.418000
6,469
6
2020-11-06-19.19.17.852000
1,645
35
2020-11-06-19.20.34.152000
1,595
65
2020-11-06-19.20.14.041000
1,522
70
2020-11-06-19.19.36.783000
4,433
70
2020-11-06-22.03.12.252000
4,431
91
2020-11-06-19.20.23.527000
8,890
Which would then result in
Product
time_stamp
Available_Qty
5
2020-11-06-19.19.33.418000
6,469
6
2020-11-06-19.19.17.852000
1,645
35
2020-11-06-19.20.34.152000
1,595
65
2020-11-06-19.20.14.041000
1,522
70
2020-11-06-22.03.12.252000
4,431
91
2020-11-06-19.20.23.527000
8,890
Hope this makes sense
Alan Shore
E-mail : ASHORE@xxxxxxxx<mailto:ASHORE@xxxxxxxx>
Phone [O] : (631) 200-5019
Phone [C] : (631) 880-8640
'If you're going through hell, keep going.'
Winston Churchill
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives at
https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link:
https://amazon.midrange.com
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link:
https://amazon.midrange.com
As an Amazon Associate we earn from qualifying purchases.