Skip to content
Advertisement

How to create temporal table from current and history?

Let’s say I have a current table that looks like this:

and I have a history table that looks as such:

How can I build a temporal table such as the following in pure SQL?

I am using a PostgresDB is dialect helps!

I know I can do this in Python, I’m stuck on calling dynamically named columns using the field from history. Would like to find an all SQL solution. Any resources to point to?

Advertisement

Answer

You can use a query like this

Please, check a demo

Here the locations table is joined with the history table to get all of the available location rows, even those which values was not ever changed, and the history table does not contain any row about them.

If you do not need it, just use the history table alone.

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