postgresql:选择timestamp列时的where / when条件

时间:2013-05-31 20:37:08

标签: postgresql

新手到postgresql这里。在8.3(不是我的选择,不能在不久的将来做任何事情)。

我从表中选择一些“时间”文本字符串并将它们插入视图中:

create or replace view test as (
    select 
    case 
    when desc like '%opening%' then 'opening'
    when desc like '%closing%' then 'closing'
    else n/a
    end as time_type, 
    to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM') where time_type = 'closing' as closing_time,
    to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM') where time_type = 'opening' as opening_time
  from source_table);

我收到错误:

ERROR:  syntax error at or near "as"
LINE 8: .../YYYY HH:MI:SS AM') where time_type = 'closing' as closing...

我之前使用过这种语法来创建其他视图让我很困惑。是因为我的where语句被错误地放置了吗?但是如果我把它们放在它们之后,它们将被普遍应用,不,这不是我想要的。

2 个答案:

答案 0 :(得分:1)

两个表达式看起来应该是case语句,例如:

case
when time_type = 'closing' then
  to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM')
else null
end as closing_time

答案 1 :(得分:0)

将以to_timestamp开头的两个投影转换为CASE语句。

相关问题