2015-10-23 100 views
0

我对postgres仍然陌生。我想在查询的SELECT部分​​有一个SELECT语句,但现在我得到一个错误。SELECT子查询PostgreSQL

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%' 

我收到以下错误:

ERROR: operator does not exist: character varying/integer LINE 3: (select to_char(to_timestamp(cua.attribute_value/1000), '... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

+0

不能分割与性格int ... –

+0

@a_horse_with_no_name有没有办法在分割之前将其更改为整数? – kya

+0

cast(cua.attribute_value as integer) –

回答

2

Apparenlty cua.attribute_value定义为varchar。错误信息告诉你,你不能用一个数字来分割一个字符串。

您需要将varchar转换(转换)为整数。你根本不需要select

这是当前的设计解决方法:

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 cast(... as bigint)运算符的Postgres特定语法。详细信息请参见the manual

但这将失败如果cua.attribute_value包含无法被转换为整数(一个空字符串''就已经打破了这个)值。

正确的解决方案是将数字存储在integer列中。不要人数非常多存储为varchar


attribute_nameattribute_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 

这将返回NULL的所有行attribute_name'timestamp'以及那些格式化的时间戳。但同样,如果该属性的值是无效的数字,这只会工作(当然,你需要调整与字符串字面'timestamp'比较中使用正确的属性名称)

+0

现在我得到以下错误:错误:整数的输入语法无效:“false” – kya

+0

我刚刚计算出来,attribute_value不仅包含以varchar存储的数字。它还包含true,false,human等。所以我需要清理数据库,以便以正确的方式存储 – kya

+0

@kya:请参阅我的编辑。如果您知道特定的属性名称只包含有效的数字,则可以使用“CASE”语句处理。 –

0

试试这个。但我不确定您是否可以将其转换为时间戳。转换为时间戳时可能会遇到另一个错误。反正给它一个尝试。

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%' 
1
SELECT cu.user_name, cu.created_date, cu.updated_date, 
cu.email_address, cua.attribute_name, cua.attribute_value, 
case when (attribute_value like '%True%' or attribute_value like '%False%') then cast(NULL as bigint) 
    else CAST(nullif(cua.attribute_value, '') AS bigint) 
    end filter_attribute_value, 
(select to_char(to_timestamp(
filter_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%' 
+0

cua.attribute_value超出整数数据类型的范围 – kya

+0

@ Kya尝试使用bigint –

+0

它工作还是我仍然收到错误.. –