我尝试使用SQL在pgAdmin中创建一个新表,因为我是一个新手,我遇到了问题。我的目标是创建一个新表(称为元数据),该表根据另一个表(称为导航)的分组来汇总值。
说导航表是这样的:
VIDEO LATITUDE
vid1 50
vid1 51
vid1 52
vid2 49
vid2 51
我需要创建一个总结导航的元数据表(包含其他一些数据);像这样的东西:
VIDEO minLATITUDE LINK
vid1 50 http://foo
vid2 49 http://bar
我一直在尝试SQL代码(基于this question):
UPDATE f1188hw.YTmeta as a
SET minLat = b.lat
FROM f1188hw.YTmeta
INNER JOIN (SELECT video, min(lat) AS lat
from f1188hw.nav
group by video) AS b
ON a.video = b.video;
pgAdmin返回:
ERROR: invalid reference to FROM-clause entry for table "a"
SQL state: 42P01
Hint: There is an entry for table "a", but it cannot be referenced from this part of the query.
Character: 158
我猜测我错过了一些明显的东西,但是通过搜索和阅读pgAdmin文档,我们无法找到它。
答案 0 :(得分:1)
使用
UPDATE f1188hw.YTmeta as a
SET a.minLat = b.lat
FROM (SELECT video, min(lat) AS lat
from f1188hw.nav
group by video) AS b
WHERE a.video = b.video;
或
UPDATE f1188hw.YTmeta
SET minLat = (SELECT min(lat)
from f1188hw.nav
where f1188hw.YTmeta.video = video
)