'How to grab the latest data via time stamp, grab the sum of one column, eliminate dups, then sum other columns

DBMS = Hadoop, Using Teradata SQL Assistant

How do I grab all the distinct records from this table based on max(tstamp), sum qty1, sumqty2 and THEN delete duplicates record based on time1,time2,time3,docnum1?

Or how can I do multiple queries and join them together? Like query1 will grab the sum values for qty1 and qty2 for unique docnums for each day. Query2 will remove duplicates based on time1,time2,time3,docnum1.Then join those two tables together on docnum1 and date?

This is the original table. There are 20 location values (c1). Each Location has a set of aisles (c2). Timestamp (c3) can have duplicates and max(tstamp) to grab the latest and greatest. DocNum1 (c4) is the new combined doc# & DocuNum2# (c5) is the old number. qty1 and qty2 are unique whereas time1, time2, time3

Location Aisle Tstamp DocNum1 DocNum2 qty1 qty2 time1 time2 time3
12 420 4/16/2021 12:22:01 PM 123 222 1 6 999 999 999
12 420 4/16/2021 11:22:01 PM 123 123 5 3 999 999 999
12 420 4/16/2021 10:22:01 PM 123 333 6 7 999 999 999
31 420 4/16/2021 12:22:01 AM 666 444 6 7 999 999 999
31 120 4/16/2021 3:22:01 PM 666 555 6 7 999 999 999
22 210 4/16/2021 01:22:01 PM 666 666 999 999 999 999 999

I used this

with query1 AS
(
   SELECT *
          , ROW_NUMBER() OVER(PARTITION BY location, aisle ORDER BY aisle, tstamp DESC) AS RowNum
   FROM   order_info
)
SELECT   location
       , aisle
       , DocNum1
       , DocNum2
       , Qty1
       , Qty2
       , time1
       , time2
       , time3
FROM   query1
JOIN date_db t2  ON to_date(tstamp) = t2.date_db
WHERE  RowNum = 1
AND t2.yearweek >= 202207

I dont know where to place the sums or how to integrate into this.

What I want to do is this:

Location Aisle Tstamp DocNum1 DocNum2 qty1 qty2 time1 time2 time3
12 420 4/16/2021 12:22:01 PM 123 222 12 16 999 999 999
31 420 4/16/2021 12:22:01 AM 666 444 6 7 999 999 999
31 120 4/16/2021 3:22:01 PM 666 555 6 7 999 999 999
22 210 4/16/2021 01:22:01 PM 666 666 999 999 999 999 999


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source