PostgreSQL: Frequency Table Expansion

喜夏-厌秋 提交于 2019-12-22 12:27:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!