Postgresql使用“where”子句计算子字符串

时间:2010-06-28 18:59:25

标签: postgresql count substring

我正在尝试计算子字符串,同时匹配另一列的不同值。以下语句给出了where子句的语法错误。

以下是否可能,以及正确的语法是什么?

select address,
       datacenter,
       ifdesc,
       count(substring(ifdesc, 'Ethernet0/*') where ifadminstatus = '1' and ifoperstatus = '1')  over (partition by address) mod0_uu,
       count(substring(ifdesc, 'Ethernet0/*') where ifadminstatus = '2') over (partition by address) mod0_ad
  from ifstatus;

1 个答案:

答案 0 :(得分:2)

这样的东西?

select address,
       datacenter,
       ifdesc,
       count(case when ifadminstatus = '1' and ifoperstatus = '1' then substring(ifdesc, 'Ethernet0/*') else null end )  over (partition by address) mod0_uu,
       count(case when ifadminstatus = '2' then substring(ifdesc, 'Ethernet0/*') else null end ) over (partition by address) mod0_ad
  from ifstatus  WHERE ifadminstatus in ('1','2');