【发布时间】:2014-06-13 15:40:45
【问题描述】:
使用查询以及 postgres 中可用的函数(例如 string_to_array 和 string_agg),将原始表中的数据转换为以下结果集。
id, text
001, {foo,boo,foo}
002, {"",for,test,friday}
003, {"","",test,friday,tuesday,foo,boo}
这里的 id 是一个人的 id,而 text 实际上是数组的类型。现在我要做的是生成以下结构。
id, text, text_count
001, foo, 2
001, boo, 1
002, test, 1
002, friday, 1
这是我用来获取我提到的现有格式的查询,但是如何增强此查询以获取 id、text、text_count 结果。
select id, string_to_array(string_agg(b.text,' '), ' ') as words
from tableA a,tableB b group by id
我也想用 "" 去掉数据,我相信它们在 postgres 中是空字符串,但不太确定。
【问题讨论】:
-
您正在尝试按 id 和 text 分组?您的原始数据是什么样的?
标签: sql postgresql