[英]PostgreSQL - left join generate_series() and table
I use generate series to create a series of numbers from 0 to 200.我使用生成系列来创建一系列从 0 到 200 的数字。
I have a table that contains dirtareas in mm² in a column called polutionmm2.我有一个表格,在名为 polutionmm2 的列中包含以 mm² 为单位的污垢区域。 What I need is to left join this table to the generated series, but the dirt area must be in cm² so /100.我需要的是将此表加入生成的系列,但污垢区域必须以 cm² 为单位,因此 /100。 I was not able to make this work, as I can't figure out how I can connect a table to a series that has no name.我无法完成这项工作,因为我不知道如何将表格连接到没有名称的系列。
This Is what I have so far:这就是我到目前为止所拥有的:
select generate_series(0,200,1) as x, cast(p.polutionmm2/100 as char(8)) as metric
from x
left join polutiondistributionstatistic as p on metric = x
error: relation X does not exist错误:关系 X 不存在
Here is some sample data: https://dbfiddle.uk/?rdbms=postgres_13&fiddle=3d7d851887adb938819d6cf3e5849719这是一些示例数据: https://dbfiddle.uk/?rdbms=postgres_13&fiddle=3d7d851887adb938819d6cf3e5849719
what I would need, is the first column (x) counting all the way from 0 to 200, and where there is a matching value, to show it in the second column.我需要的是第一列 (x) 从 0 一直计数到 200,并且有一个匹配值,以便在第二列中显示它。
Like this:像这样:
x, metric
0, 0
1, 1
2, 2
3, null
4, 4
5, null
... , ...
... , ...
200, null
You can put generate_series()
in the FROM
.您可以将generate_series()
放在FROM
中。 So, I think you want something like this:所以,我认为你想要这样的东西:
select gs.x, cast(p.polutionmm2/100 as char(8)) as metric
from generate_series(0,200,1) gs(x) left join
p
on gs.x = (p.polutionmm2/100);
I imagine there is also more to your query, because this doesn't do much that is useful.我想您的查询还有更多内容,因为这并没有多大用处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.