Skip to content
Advertisement

How can I ‘flatten’ a one to many table with URLs so that each additional URL shows up in a new column?

I’m trying to ‘flatten’ a one to many relationship using SQL to create a CSV of points and their associated photos to use with a web map.

Table 1 is a list of points and their locations, and Table 2 is a list of URLs of photos and their associated captions.

How do I write SQL code so that it creates a table that looks like this?

Advertisement

Answer

You want to pivot the data in table2. But to do so, you need a pivoting column, which can be generated using row_number().

I prefer to use conditional aggregation for pivoting, so:

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