1

Like here, I need to dynamically add columns depended on the counts of occurrences.

The differences are:

  • There is no orders column. No other table is joined or any foreign keys.
  • Additional columns are fixed and user specified. In the following example, it is set to 2.

Example Table:

+--+-----+
|id|data | // + Any other Constant rows
+--+-----+
|1 |P    | // 1. of id1
+--+-----+ 
|1 |Q    | // 2. of id1
+--+-----+
|2 |R    | // 1. of id2
+--+-----+
|3 |S    | // 1. of id3
+--+-----+
|3 |a    | // 2. of id3
+--+-----+
|3 |C    | // 3. of id3
+--+-----+

Example result table with 2 data columns (which is pre set by user - of course it also could be 3 or more)

+--+-----+-----+
|id|data1|data2| // + all other constant columns
+--+-----+-----+
|1 |P    |Q    | // exactly 2 entries
+--+-----+-----+
|2 |R    |NULL | // no second entry
+--+-----+-----+
|3 |S    |a    | // no third entry
+--+-----+-----+

My first idea was to create an identifying column, like the "orders" columns, but I couldn't find a way to accomplish that.

Is it possible to realize that? I prepared a sqlFiddle

Community
  • 1
  • 1
Mruf
  • 766
  • 7
  • 16

0 Answers0