2

Does anyone know how to expand a frequency table in PostgreSQL?

For example, transform table x:

data   | frequency
-------+-----------
string |         4

into

data   | index 
-------+-------
string |     1
string |     2
string |     3
string |     4

Set up code:

CREATE TABLE x (
  data TEXT,
  frequency INTEGER
);
INSERT INTO x VALUES ('string',4);
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Martin Velez
  • 1,379
  • 11
  • 24

1 Answers1

5

This is amazingly simple with generate_series():

SELECT data, generate_series(1, frequency) AS index
FROM   x;
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228