Skip to content
Advertisement

Transpose table with “list” of the same attributes

Is it possible to transpose a table with repeated columns?

Existing table:

Cross tab or solution based on

is not applicable in this scenario because always is match last occurance of repeated attribute.

I want to perform search on the question body but I don’t know how without a transposed table.
E.g. I want to find user_id where question1='...' AND question2='...'

Advertisement

Answer

This is a case of relational division. Two example query techniques to get what you want:

Or:

-> sqlfiddle demo

Find many more under this related question:
How to filter SQL results in a has-many-through relation

crosstab() from the additional module tablefunc is just a way to display your data in a modified way. But you are looking for a query technique. It’s not a display issue.
You can find many examples for crosstab() here on SO, too, with a search like this one.

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