【发布时间】:2016-08-10 10:52:40
【问题描述】:
我正在尝试使用 postgresql 来汇总一些数据,并且需要生成一个频率列,然后用频率的结果转置另一列。
例如 我的起始表是这样的:
Month | Nationality | Car
Oct-15 | GBR | Rover
Sep-15 | FRA | Fiat
Oct-15 | GBR | Rover
Sep-15 | TUR | Fiat
我想创建一个新列来计算其他列的每个唯一组合的频率。所以它会像这样:
Month | Nationality | Car | FREQ
Oct-15 | GBR | Rover | 2
Sep-15 | FRA | Fiat | 1
Sep-15 | TUR | Fiat | 1
然后我想转置 Month 列,为 Month 中的每个值创建新列,并使用频率计数填充这些列的值:
Nationality | Car | Sep-15 | Oct-15
GBR | Rover | 0 | 2
FRA | Fiat | 1 | 0
TUR | Fiat | 1 | 0
我一直在研究进行转置查询和交叉表函数,但无法弄清楚如何使用唯一组合的频率作为值来使其工作。
谢谢
【问题讨论】:
标签: sql postgresql frequency transpose