简体   繁体   English

根据另一个表中组的最小值在表中设置值

[英]Set values in table based on minimum from groups in another table

I'm trying to create a new table in pgAdmin using SQL, and since I'm a total novice I'm running into problems. 我正在尝试使用SQL在pgAdmin中创建一个新表,由于我是一个新手,所以遇到了问题。 My goal is to create a new table (called metadata) that summarizes values based on groupings from another table (called navigation). 我的目标是创建一个新表(称为元数据),该表基于来自另一个表(称为导航)的分组来汇总值。

Say the navigation table is like this: 说导航表是这样的:

VIDEO  LATITUDE
vid1   50
vid1   51
vid1   52
vid2   49
vid2   51  

I need to create a metadata table that summarizes the navigation (with some other data); 我需要创建一个元数据表来汇总导航(以及其他一些数据); something like this: 像这样的东西:

VIDEO  minLATITUDE   LINK
vid1        50       http://foo
vid2        49       http://bar  

I've been trying the SQL code (based on this question ): 我一直在尝试SQL代码(基于此问题 ):

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;

And pgAdmin returns: 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

I'm guessing I'm missing something obvious, but haven't been able to find it through searching and reading through pgAdmin documentation. 我猜想我遗漏了一些明显的东西,但是还没有通过搜索和阅读pgAdmin文档来找到它。

Use 采用

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;

Or 要么

UPDATE f1188hw.YTmeta
SET minLat = (SELECT min(lat) 
              from f1188hw.nav
              where f1188hw.YTmeta.video = video 
             ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM