Skip to content
Advertisement

Postgresql SELECT sample with FOR loop

I have a task where I need to create a 1000 sample row limit query for each attribute selected.

So for example if I have:

I would need to turn this in to:

Problem is that I have a lot of these attributes and that I might be adding more attributes later. Is there a better way of writing this? Like maybe with a FOR loop that iterates through an array of strings to get everything at once? I tried the following but I don’t really know how to do it properly:

Advertisement

Answer

You can use row_number():

You could also use a lateral join:

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