在 PostgreSQL 中,我如何遍历表 A,并使用表 A 中的列作为连接到表 B 的限制以填充表 C

问题描述 投票:0回答:0

使用 Greenplum 6(基于 PostgreSQL 9.4),我有这些 样品表:

桌子

SAMPLE_A

| uniqueId | SampleSz |
| -------- | -------- |
| 1        | 25       |
| 2        | 50450    |
| 3        | 9        |

桌子

SAMPLE_B

| IP       | uniqueId |
| -------- | -------- |
| 1.4.4.5  | (1,2,3)  |
| 2.5.6.7  | (2)      |
| 3.4.7.8  | (1,3)    |

我正在尝试从上面创建一个新表:
对于

uniqueId=1
,拉取一组随机的25(
SAMPLE_A.SampleSz
)个IP(
SAMPLE_B.IP
),其中
SAMPLE_A.uniqueId
SAMPLE_B.uniqueId
的数组中。然后迭代到下一个
SAMPLE_A.uniqueId
并随机拉取50450 ...等
实际表格有点复杂,但我被困在这里。

我试图为一个单一的记录写这篇文章(但失败了):

select i.ip, s.uniqueId
from SAMPLE_A s
join lateral (
   select distinct ip
   from SAMPLE_B i
   where s.uniqueId = any(i.uniqueId)
-- ORDER BY random()
   LIMIT s.SampleSz
   ) i on true

这会抛出一个解组错误。即使有效,也不能解决我的全部问题,但我认为这是第一步。

postgresql loops database-cursor greenplum
© www.soinside.com 2019 - 2024. All rights reserved.