【发布时间】:2017-01-25 05:41:40
【问题描述】:
我有以下疑问:
SELECT
"external_id" AS EVENT_ID,
"uuid" AS EVENT_VERSION_ID,
"timestamp" AS EVENT_VERSION_TIMESTAMP,
CONCAT('{',
'"timestamp": ', COALESCE(timestamp, NULL), ',',
'"uuid": ', COALESCE("uuid", '"' || "uuid" || '"', 'null'), ',',
'"fraud": ', COALESCE("fraud",'true','false', NULL), ',',
'"score": ', COALESCE("score", NULL), ',',
'"scoreTracking": ', COALESCE("scoreTracking", NULL), ',',
'"domain": ', COALESCE("domain", '"' || "domain" || '"', NULL), ',',
'"event_occurred_at": ', COALESCE("event_occurred_at", NULL), ',',
'"event_received_at": ', COALESCE("event_received_at", NULL), ',',
'"event_type": ', COALESCE("event_type", '"' || "event_type" || '"', NULL), ',',
'"event_is_update": ', COALESCE("event_is_update",'true', 'false', NULL), ',',
'"transaction_id": ', COALESCE("transaction_id", '"' || "transaction_id" || '"', NULL), ',',
'"transaction_created_at": ', COALESCE("transaction_created_at", NULL), ',',
'"transaction_updated_at": ', COALESCE("transaction_updated_at", NULL), ',',
'"transaction_type": ', COALESCE("transaction_type", '"' || "transaction_type" || '"', NULL), ',',
.
.
.
.
.
186 fields to concat......) AS EVENT_FIELDS;
但是,postgres 不允许我传递超过 100 个参数。我在网上查了一下,发现阅读了 "variadic" 参数。但是,我不确定如何在这种情况下使用它。
提前非常感谢。
【问题讨论】:
-
你能不能把
CONCAT100 和CONCAT86 合并两个结果? -
顺便说一句。为什么
NULL是您对coalesce的论据之一? -
@Martin 我不这么认为,因为我必须将所有字段连接到相同的字段中,例如 EVENT_FIELDS。
-
@MartinSmith 嗯,所以当第一个参数为 null 时,我希望它插入 NULL 而不是空值。或者我是这么理解的。我在这里有什么误解吗?
-
看起来您正在尝试创建 JSON 文档。为什么不使用
row_to_json()或json_build_object()?
标签: sql postgresql concatenation