添加一个新的表列,并从现有列中提取数据

发布于 2025-01-25 22:37:01 字数 1410 浏览 3 评论 0原文

要序言,我正在使用Postgis和Pgadmin4来完成所有这些工作。

因此,我将一组横司导入到具有“ RID”,“ Raster”和“ Filename”列的数据库中。 我使用此查询添加了列,在这里我很容易使用PGADMIN中的GUI实现。

ALTER TABLE IF EXISTS ch12.prec
ADD COLUMN "Month" integer;

我需要从文件名中提取月份,然后将其插入此新列中,但对如何做到这一点。文件名结构为“ prec1_16.bil”,其中1个是一个月,而年度为16。我尝试使用insert语句与case表达式说明:

INSERT INTO ch12.prec (Month)
SELECT
 CASE
    WHEN filename = 'prec1_16.bil' OR filename = 'prec1_17.bil' THEN '1'
    WHEN filename = 'prec2_16.bil' OR filename = 'prec2_17.bil' THEN '2'
    WHEN filename = 'prec3_16.bil' OR filename = 'prec3_17.bil' THEN '3'
    WHEN filename = 'prec4_16.bil' OR filename = 'prec4_17.bil' THEN '4'
    WHEN filename = 'prec5_16.bil' OR filename = 'prec5_17.bil' THEN '5'
    WHEN filename = 'prec6_16.bil' OR filename = 'prec6_17.bil' THEN '6'
    WHEN filename = 'prec7_16.bil' OR filename = 'prec7_17.bil' THEN '7'
    WHEN filename = 'prec8_16.bil' OR filename = 'prec8_17.bil' THEN '8'
    WHEN filename = 'prec9_16.bil' OR filename = 'prec9_17.bil' THEN '9'
    WHEN filename = 'prec10_16.bil' OR filename = 'prec10_17.bil' THEN '10'
    WHEN filename = 'prec11_16.bil' OR filename = 'prec11_17.bil' THEN '11'
    WHEN filename = 'prec12_16.bil' OR filename = 'prec12_17.bil' THEN '12'
 END filename
FROM ch12.prec;

尽管能够看到并查询它,但我得到的只是列不存在错误。有什么想法吗? update之类的东西会更合适吗?

To preface, I'm using PostGIS and pgAdmin4 to do all of this work.

So I've imported a set of rasters into a database with the "rid", "raster" and "filename" columns.
I used this query to add the column, where I just as easily could have used the GUI in pgAdmin to achieve the same.

ALTER TABLE IF EXISTS ch12.prec
ADD COLUMN "Month" integer;

I need to extract the month from the filename and insert it into this new column but am at a loss on how to do so. The file name structure is "prec1_16.bil", where the 1 is the month and 16 the year. I tried using an INSERT statement with a CASE expression like:

INSERT INTO ch12.prec (Month)
SELECT
 CASE
    WHEN filename = 'prec1_16.bil' OR filename = 'prec1_17.bil' THEN '1'
    WHEN filename = 'prec2_16.bil' OR filename = 'prec2_17.bil' THEN '2'
    WHEN filename = 'prec3_16.bil' OR filename = 'prec3_17.bil' THEN '3'
    WHEN filename = 'prec4_16.bil' OR filename = 'prec4_17.bil' THEN '4'
    WHEN filename = 'prec5_16.bil' OR filename = 'prec5_17.bil' THEN '5'
    WHEN filename = 'prec6_16.bil' OR filename = 'prec6_17.bil' THEN '6'
    WHEN filename = 'prec7_16.bil' OR filename = 'prec7_17.bil' THEN '7'
    WHEN filename = 'prec8_16.bil' OR filename = 'prec8_17.bil' THEN '8'
    WHEN filename = 'prec9_16.bil' OR filename = 'prec9_17.bil' THEN '9'
    WHEN filename = 'prec10_16.bil' OR filename = 'prec10_17.bil' THEN '10'
    WHEN filename = 'prec11_16.bil' OR filename = 'prec11_17.bil' THEN '11'
    WHEN filename = 'prec12_16.bil' OR filename = 'prec12_17.bil' THEN '12'
 END filename
FROM ch12.prec;

All I get is a column doesn't exist error, despite being able to see it and query it. Any thoughts? Would something like UPDATE be more appropriate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2025-02-01 22:37:01

在这里,您双引号列名称“月”,从而保存混合案例拼写:

 如果存在CH12.PREC
添加列“月”整数;
 

在这里,您没有,从而降低标识符:

 插入CH12.PREC(月)...
 

参阅:

请 ,您正在寻找 update ,而不是insert

UPDATE ch12.prec
SET   "Month" = substring(filename, '^prec(1?\d)_1[67].bil

用较短的 case 表达式“ https://www.postgresql.org/docs/current/functions-matching.html#functions-posix-regexp“ rel =“ nofollow noreferrer”>正常表达式(100%同等)。并使其成为实际的整数

并不意味着我会这样做。如果可以避免使用混合案例标识符。而且我不添加带有冗余信息的列(特殊情况除外)。
考虑而不是添加列并用我给您的表达式操作 - 或者如果可以做出假设,则可能更简单。

)::int;

用较短的 case 表达式“ https://www.postgresql.org/docs/current/functions-matching.html#functions-posix-regexp“ rel =“ nofollow noreferrer”>正常表达式(100%同等)。并使其成为实际的整数

并不意味着我会这样做。如果可以避免使用混合案例标识符。而且我不添加带有冗余信息的列(特殊情况除外)。
考虑而不是添加列并用我给您的表达式操作 - 或者如果可以做出假设,则可能更简单。

Here you double-quoted the column name "Month", thereby preserving mixed-case spelling:

ALTER TABLE IF EXISTS ch12.prec
ADD COLUMN "Month" integer;

Here you didn't, thereby lower-casing the identifier:

INSERT INTO ch12.prec (Month) ...

See:

And yes, you are looking for UPDATE, not INSERT:

UPDATE ch12.prec
SET   "Month" = substring(filename, '^prec(1?\d)_1[67].bil

Replacing your lengthy CASE expression with a shorter regular expression (100 % equivalent). And making it an actual integer.

Doesn't mean I would do that. I don't use mixed-case identifiers if I can avoid it. And I don't add columns with redundant information (except for special cases).
Consider not adding a column and operating with the expression I gave you instead - or possibly an even simpler one if assumptions can be made.

)::int;

Replacing your lengthy CASE expression with a shorter regular expression (100 % equivalent). And making it an actual integer.

Doesn't mean I would do that. I don't use mixed-case identifiers if I can avoid it. And I don't add columns with redundant information (except for special cases).
Consider not adding a column and operating with the expression I gave you instead - or possibly an even simpler one if assumptions can be made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文