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
  • 539,169
  • 125
  • 977
  • 1,137
Martin Velez
  • 1,330
  • 9
  • 24

1 Answers1

5

This is amazingly simple with generate_series():

SELECT data, generate_series(1, frequency) AS index
FROM   x;
Erwin Brandstetter
  • 539,169
  • 125
  • 977
  • 1,137