I am trying to convert a table such as seen below in a dynamic way to construct a traceability chain model of batch throughout my process (success of tanks)
After some tests in excel, I went with SQL because I am working on a database and I thought the looping function would be the way. I am looking for help because my code is getting overly complex for me (first project in SQL) and maybe I am missing a simpler solution with or without PostgreSQL.
Right now, I am stuck in the dynamic function that would create a table with as many columns as unique tanks and populate them based on the joins of multiple tables. Any help would be appreciated, Thanks!
Database table:
US tank | DS tank | US batch n° | DS batch n° |
---|---|---|---|
citerne | B430 | 122 | 55 |
B430 | K4 | 55 | 603 |
US means UpStream and DS DownStream
Result table expected:
citerne | B430 | K4 |
---|---|---|
122 | 55 | 603 |
there is a finite number of tanks but an infinite number of the batch numbers.
Here is a schema of my method : https://i.stack.imgur.com/bl7hx.png
Here is a fiddle of my data and expected result : https://dbfiddle.uk/wG9ghV_P
Here is the result expected from this data (also in the fiddle): https://i.stack.imgur.com/2OQhS.png