PostgreSQL:将行拆分为4行 [英] Postgresql: Split line into 4 lines

查看:316
本文介绍了PostgreSQL:将行拆分为4行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关心的是使用SQL脚本将一行分成4行。
是我获得了SQL结果的年份,季度,月份和x值。现在,我也想花费一个月中的第一个星期(1-4),而不必将其添加为表格的一列。
同样,该值应除以四。
因此,根据以下结果:

My concern is to split a single line into 4 lines using a SQL script. It is that I get in an SQL Result the year, the quarter, the month and a x worthy value. Now I would also like to spend the week of the month (1-4) without having to add this as a column of the table. Likewise, the value should be divided by four. Thus, from this result:

year | quarter | month | value
2016 | 1       | 1     | 78954

结果:

year | quarter | month | week | value
2016 | 1       | 1     | 1    | 19738,5
2016 | 1       | 1     | 2    | 19738,5
2016 | 1       | 1     | 3    | 19738,5
2016 | 1       | 1     | 4    | 19738,5

我不知道该如何实现。
我希望有人能帮助我。

I have no idea how I could implement this. I hope anyone can help me.

最诚挚的问候

推荐答案

您可以使用笛卡尔联接来做到这一点:

You could do it with a cartesian join:

SELECT a.year, a.quarter, a.month, b.week, a.value
  FROM a, (SELECT UNNEST(ARRAY[1, 2, 3, 4]) as week) b

这篇关于PostgreSQL:将行拆分为4行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆