问题
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);
回答1:
This is amazingly simple with generate_series():
SELECT data, generate_series(1, frequency) AS index
FROM x;
来源:https://stackoverflow.com/questions/12327046/postgresql-frequency-table-expansion