I have two tables below are the fields for each of them. They have one field in common which is uniq_id. The requirement is something like to join two tables and then do a sum a numeric amount based max DATE and group by should be done based 3 fields. Which belongs two different tables.
Table1 | |||
---|---|---|---|
uniq_id | round | date | name |
AZ08252020 | round1 | 08/25/2020 | AZEK |
AZ08252021 | round1 | 08/25/2021 | AZEK |
AZ08252022 | round5 | 08/25/2022 | AZEK |
AZ08252023 | round3 | 08/25/2023 | AZEK |
AZ08252024 | round3 | 08/25/2024 | AZEK |
AZ08252025 | round3 | 08/25/2025 | AZEK |
BX08252020 | round1 | 08/25/2020 | BXCF |
BX08252021 | round1 | 08/25/2021 | BXCF |
BX08252022 | round2 | 08/25/2022 | BXCF |
BX08252023 | round2 | 08/25/2023 | BXCF |
BX08252024 | round7 | 08/25/2024 | BXCF |
BX08252025 | round7 | 08/25/2025 | BXCF |
Table2 | ||||
---|---|---|---|---|
uniq_id | dept | Person NAME | amount | date |
AZ08252020 | 1 | xyz | 200 | 08/25/2020 |
AZ08252021 | 1 | xyz | 210 | 08/25/2021 |
AZ08252022 | 1 | xyz | 300 | 08/25/2022 |
AZ08252023 | 1 | xyz | 300 | 08/25/2023 |
AZ08252024 | 1 | xyz | 320 | 08/25/2024 |
AZ08252025 | 1 | xyz | 310 | 08/25/2025 |
BX08252020 | 2 | abc | 200 | 08/25/2020 |
BX08252021 | 2 | abc | 210 | 08/25/2021 |
BX08252022 | 2 | abc | 300 | 08/25/2022 |
BX08252023 | 2 | abc | 300 | 08/25/2023 |
BX08252024 | 2 | abc | 320 | 08/25/2024 |
BX08252025 | 2 | abc | 310 | 08/25/2025 |
I want to get the total amount for each dept,each round, person name(assuming one person in each dept, if more than 1 person , then we have sum that across dept)
only for the latest date. So if round1 is present two times, i would only want the most recent one.
My desired output should look like below
uniq_id | round | date | name | dept | Person NAME | amount |
---|---|---|---|---|---|---|
AZ08252021 | round1 | 08/25/2021 | AZEK | 1 | xyz | 210 |
AZ08252022 | round5 | 08/25/2022 | AZEK | 1 | xyz | 300 |
AZ08252025 | round3 | 08/25/2025 | AZEK | 1 | xyz | 310 |
BX08252021 | round1 | 08/25/2021 | BXCF | 2 | abc | 210 |
BX08252023 | round2 | 08/25/2023 | BXCF | 2 | abc | 300 |
BX08252025 | round7 | 08/25/2025 | BXCF | 2 | abc | 310 |