Skip to content
Advertisement

How to join two tables while only selecting the highest day of each month from one table

I have two tables. One with metadata, one with billing data. I need to join those effiently in order to assign metadata to costs.

Table 1 (metadata) looks like this:

Table 2 (billing data) looks like this:

For each combination of year, month, id in Table 2, there is an corresponding ID in Table 1.

For each year, month, id in T2, i need label1, label2 from the row in T1 which matches year, month, id, and has the highest date (in that month), so that the result may look like this:

I.e. the first row of T1 is not used, as the second row has labels with a newer date in that month.

I am using Atheana on Amazon Webservices, which should be Presto compatible, I think.

How do I select this correctly? Preferably, in a way that can be used as a view.

Advertisement

Answer

You can use row_number() to get to the last row in a month:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement