Skip to content
Advertisement

How to query: “for which do these values apply”?

I’m trying to match and align data, or resaid, count occurrences and then list for which values those occurrences occur.

Or, in a question: “How many times does each ID value occur, and for what names?”

For example, with this input

I want the output to be:

Or similarly, the input could be (noticing that the ID values are not aligned),

but that seems to complicate things.

As close as I am to the desired results is in SQL, as

which outputs

I’ll appreciate help.

Advertisement

Answer

You seem to want a pivot. In SQL, you have to specify the number of columns in advance (unless you construct the query as a string).

But the idea is:

If you have an unknown number of columns, you can aggregate the values into an array:

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