添加一个新的表列,并从现有列中提取数据
要序言,我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您双引号列名称
“月”
,从而保存混合案例拼写:在这里,您没有,从而降低标识符:
参阅:
请 ,您正在寻找
update
,而不是insert
:用较短的 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:Here you didn't, thereby lower-casing the identifier:
See:
And yes, you are looking for
UPDATE
, notINSERT
:Replacing your lengthy
CASE
expression with a shorter regular expression (100 % equivalent). And making it an actualinteger
.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.