选择子查询PostgreSQL
我对博士后还是新手。我希望在查询的SELECT部分有一个SELECT语句,但现在我得到一个错误选择子查询PostgreSQL,sql,postgresql,subquery,Sql,Postgresql,Subquery,我对博士后还是新手。我希望在查询的SELECT部分有一个SELECT语句,但现在我得到一个错误 SELECT cu.user_name, cu.created_date, cu.updated_date, cu.email_address, cua.attribute_name, cua.attribute_value, (select to_char(to_timestamp(cua.attribute_value / 1000), 'yyyy-mm-dd HH24
SELECT cu.user_name, cu.created_date, cu.updated_date, cu.email_address,
cua.attribute_name, cua.attribute_value,
(select to_char(to_timestamp(cua.attribute_value / 1000), 'yyyy-mm-dd HH24:MI:SS')) AS Issue_Update
FROM cwd_user cu
INNER JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%'
我得到以下错误:
错误:运算符不存在:字符变化/整数
第3行:(选择to_char(to_timestamp(cua.attribute_value/1000))。。。
^
提示:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型转换
试试这个。但是我不确定你是否能把它转换成时间戳。你在转换成时间戳时可能会遇到另一个错误。无论如何,试试看
SELECT cu.user_name, cu.created_date, cu.updated_date,
cu.email_address, cua.attribute_name, cua.attribute_value,
(select to_char(to_timestamp(cast(cua.attribute_value as double precision) / 1000), 'yyyy-mm-dd HH24:MI:SS')) AS Issue_Update
FROM cwd_user cu
INNER JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%'
试试这个。但是我不确定你是否能把它转换成时间戳。你在转换成时间戳时可能会遇到另一个错误。无论如何,试试看
SELECT cu.user_name, cu.created_date, cu.updated_date,
cu.email_address, cua.attribute_name, cua.attribute_value,
(select to_char(to_timestamp(cast(cua.attribute_value as double precision) / 1000), 'yyyy-mm-dd HH24:MI:SS')) AS Issue_Update
FROM cwd_user cu
INNER JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%'
试试这个。但是我不确定你是否能把它转换成时间戳。你在转换成时间戳时可能会遇到另一个错误。无论如何,试试看
SELECT cu.user_name, cu.created_date, cu.updated_date,
cu.email_address, cua.attribute_name, cua.attribute_value,
(select to_char(to_timestamp(cast(cua.attribute_value as double precision) / 1000), 'yyyy-mm-dd HH24:MI:SS')) AS Issue_Update
FROM cwd_user cu
INNER JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%'
试试这个。但是我不确定你是否能把它转换成时间戳。你在转换成时间戳时可能会遇到另一个错误。无论如何,试试看
SELECT cu.user_name, cu.created_date, cu.updated_date,
cu.email_address, cua.attribute_name, cua.attribute_value,
(select to_char(to_timestamp(cast(cua.attribute_value as double precision) / 1000), 'yyyy-mm-dd HH24:MI:SS')) AS Issue_Update
FROM cwd_user cu
INNER JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%'
显然
cua.attribute\u value
定义为varchar
。错误消息告诉您不能将字符串除以数字
您需要将varchar转换(强制转换)为整数。而且您根本不需要选择
这是当前设计的变通方法:
SELECT cu.user_name,
cu.created_date,
cu.updated_date,
cu.email_address,
cua.attribute_name,
cua.attribute_value,
to_char(to_timestamp(cua.attribute_value::bigint / 1000), 'yyyy-mm-dd HH24:MI:SS') AS Issue_Update
FROM cwd_user cu
JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%';
::bigint
将字符串强制转换为整数值。这是ANSI SQL强制转换(…作为bigint)
运算符的Postgres特定语法。有关详细信息,请参阅
但是,如果cua.attribute_value
包含无法转换为整数的值(空字符串'
会破坏此值),则此操作将失败
正确的解决方案是将数字存储在integer
列中。不要将数字存储为varchar
attribute\u name
和attribute\u value
听起来非常像被称为“实体属性值”的(反)模式
如果确定具有特定名称的属性的时间戳信息是正确的,则可以执行类似操作以避免强制转换错误:
CASE WHEN cua.attribute_name = 'timestamp' THEN
to_char(to_timestamp(cua.attribute_value::bigint / 1000), 'yyyy-mm-dd HH24:MI:SS')
END AS Issue_Update
对于属性_name
不是'timestamp'
的所有行,这将返回NULL
,对于那些不是'timestamp'
的行,这将返回格式化的时间戳。但同样,这仅在该属性的值为有效数字时才起作用(当然,您需要调整与字符串literal的比较'timestamp'
以使用正确的属性名称)显然cua。属性值定义为varchar
。错误消息告诉您不能将字符串除以数字
您需要将varchar转换(强制转换)为整数。而且您根本不需要选择
这是当前设计的变通方法:
SELECT cu.user_name,
cu.created_date,
cu.updated_date,
cu.email_address,
cua.attribute_name,
cua.attribute_value,
to_char(to_timestamp(cua.attribute_value::bigint / 1000), 'yyyy-mm-dd HH24:MI:SS') AS Issue_Update
FROM cwd_user cu
JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%';
::bigint
将字符串强制转换为整数值。这是ANSI SQL强制转换(…作为bigint)
运算符的Postgres特定语法。有关详细信息,请参阅
但是,如果cua.attribute_value
包含无法转换为整数的值(空字符串'
会破坏此值),则此操作将失败
正确的解决方案是将数字存储在integer
列中。不要将数字存储为varchar
attribute\u name
和attribute\u value
听起来非常像被称为“实体属性值”的(反)模式
如果确定具有特定名称的属性的时间戳信息是正确的,则可以执行类似操作以避免强制转换错误:
CASE WHEN cua.attribute_name = 'timestamp' THEN
to_char(to_timestamp(cua.attribute_value::bigint / 1000), 'yyyy-mm-dd HH24:MI:SS')
END AS Issue_Update
对于属性_name
不是'timestamp'
的所有行,这将返回NULL
,对于那些不是'timestamp'
的行,这将返回格式化的时间戳。但同样,这仅在该属性的值为有效数字时才起作用(当然,您需要调整与字符串literal的比较'timestamp'
以使用正确的属性名称)显然cua。属性值定义为varchar
。错误消息告诉您不能将字符串除以数字
您需要将varchar转换(强制转换)为整数。而且您根本不需要选择
这是当前设计的变通方法:
SELECT cu.user_name,
cu.created_date,
cu.updated_date,
cu.email_address,
cua.attribute_name,
cua.attribute_value,
to_char(to_timestamp(cua.attribute_value::bigint / 1000), 'yyyy-mm-dd HH24:MI:SS') AS Issue_Update
FROM cwd_user cu
JOIN cwd_user_attribute cua ON cu.id = cua.user_id
WHERE cu.user_name LIKE 'perter%';
::bigint
将字符串强制转换为整数值。这是ANSI SQL强制转换(…作为bigint)
运算符的Postgres特定语法。有关详细信息,请参阅
但是,如果cua.attribute_value
包含无法转换为整数的值(空字符串'
会破坏此值),则此操作将失败
正确的解决方案是将数字存储在integer
列中。不要将数字存储为varchar
attribute\u name
和attribute\u value
听起来非常像被称为“实体属性值”的(反)模式
如果确定具有特定名称的属性的时间戳信息是正确的,则可以执行类似操作以避免强制转换错误:
CASE WHEN cua.attribute_name = 'timestamp' THEN
to_char(to_timestamp(cua.attribute_value::bigint / 1000), 'yyyy-mm-dd HH24:MI:SS')
END AS Issue_Update
对于属性_name
不是'timestamp'
的所有行,这将返回NULL
,对于那些不是'timestamp'
的行,这将返回格式化的时间戳。但同样,这仅在该属性的值为有效数字时才起作用(当然,您需要调整与字符串literal的比较'timestamp'
以使用正确的属性名称)显然cua。属性值定义为varchar
。错误消息告诉您不能将字符串除以数字
您需要将varchar转换(强制转换)为整数。而且您根本不需要选择
这是wo