database - UPDATE on QuestDB using VALUES syntax - Stack Overflow

I want to update some values on a table using the VALUES syntax, as supported by INSERT. When I try thi

I want to update some values on a table using the VALUES syntax, as supported by INSERT. When I try this I get an error saying ) expected

update t
SET 
  name = myvalues.name
FROM (
  VALUES
    (1, 'textA'),
    (2, 'textB')
) AS myvalues (id, name)
WHERE t.id = myvalues.id

My table schema is like this

create table t (
  timestamp timestamp,
  id symbol,
  name varchar,
  price float
) timestamp(timestamp) partition by day DEDUP UPSERT KEYS(timestamp, id);

And I can insert values with the VALUES syntax as in:

insert into t
values 
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'APPL', 'apple', 102.1),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'GOOG', 'google', 45),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'APPL', 'apple', 102.1),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'APPL', 'apple', 102.3),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'GOOG', 'google', 45.1),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'MSFT', 'microsoft', 70);

What would be the proper way to run the update?

I want to update some values on a table using the VALUES syntax, as supported by INSERT. When I try this I get an error saying ) expected

update t
SET 
  name = myvalues.name
FROM (
  VALUES
    (1, 'textA'),
    (2, 'textB')
) AS myvalues (id, name)
WHERE t.id = myvalues.id

My table schema is like this

create table t (
  timestamp timestamp,
  id symbol,
  name varchar,
  price float
) timestamp(timestamp) partition by day DEDUP UPSERT KEYS(timestamp, id);

And I can insert values with the VALUES syntax as in:

insert into t
values 
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'APPL', 'apple', 102.1),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'GOOG', 'google', 45),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'APPL', 'apple', 102.1),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'APPL', 'apple', 102.3),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'GOOG', 'google', 45.1),
( rnd_timestamp(to_timestamp('2025-02-21', 'yyyy-MM-dd'), to_timestamp('2025-02-21T01:00:00', 'yyyy-MM-ddTHH:mm:ss'), 0), 'MSFT', 'microsoft', 70);

What would be the proper way to run the update?

Share Improve this question asked Feb 21 at 17:42 Javier RamirezJavier Ramirez 4,0951 gold badge27 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Since we are using DEDUP on the table, one efficient way of doing this is by re-inserting the affected rows with the updated value.

Since we want to join with the value list, we can do first a UNION with the selected values, then JOIN with the original table, and do an INSERT INTO from that subquery.

with vals AS (
SELECT 'APPL' as id, 'APPLE Inc' as name
UNION
SELECT 'GOOG' as id, 'Google' as name
), updated AS (
SELECT 
t.timestamp, t.id, vals.name as name, t.price
FROM
t JOIN vals
ON(id)
)
INSERT BATCH 100000 INTO t (timestamp, id, name, price) SELECT * FROM updated;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163099a4614490.html

相关推荐

  • database - UPDATE on QuestDB using VALUES syntax - Stack Overflow

    I want to update some values on a table using the VALUES syntax, as supported by INSERT. When I try thi

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信