开发者

multiple count(substring) with windowing psql 8.4.4

开发者 https://www.devze.com 2023-01-05 02:36 出处:网络
I am trying to create the following view, and I get the error below: I am able to do 1 count statement (if I remove the AS \"Mod0\") Is it possible to count multiple substrings, with the output count

I am trying to create the following view, and I get the error below: I am able to do 1 count statement (if I remove the AS "Mod0") Is it possible to count multiple substrings, with the output count to a new column?

create view portcnt as 
    select 
    address,
    datacenter,
    ifdesc,
    count(substring(ifdesc, 'Ethernet0/*')) as "Mod0",
    count(substring(ifdesc, 'Ethernet1/*')) as "Mod1",
    count(substring(ifdesc, 'Ethernet2/*')) as "Mod2",
    count(substring(ifdesc, 'Ethernet3/*')) as "Mod3",
    count(substring(ifdesc, 'Ethernet4/*'开发者_如何学Go)) as "Mod4",
    count(substring(ifdesc, 'Ethernet5/*')) as "Mod5",
    count(substring(ifdesc, 'Ethernet6/*')) as "Mod6"
    over (partition by address)
    from ifstatus where datacenter = 'DC' 
    and ifadminstatus = '1' and ifoperstatus = '1';
ERROR:  syntax error at or near "by"
LINE 13:  over (partition by address)


found a solution that worked:

create view portcnt1 as
    select
    address,
    datacenter,
    ifdesc,
    count(substring(ifdesc, 'Ethernet0/*')) 
    over (partition by address) mod0,
    count(substring(ifdesc, 'Ethernet1/*'))
    over (partition by address) mod1,
    count(substring(ifdesc, 'Ethernet2/*'))
    over (partition by address) mod2,
    count(substring(ifdesc, 'Ethernet3/*'))
    over (partition by address) mod3,
    count(substring(ifdesc, 'Ethernet4/*'))
    over (partition by address) mod4,
    count(substring(ifdesc, 'Ethernet5/*'))
    over (partition by address) mod5,
    count(substring(ifdesc, 'Ethernet6/*'))
    over (partition by address) mod6,
    count(substring(ifdesc, 'Ethernet7/*'))
    over (partition by address) mod7,
    count(substring(ifdesc, 'Ethernet8/*'))
    over (partition by address) mod8,
    count(substring(ifdesc, 'Ethernet9/*'))
    over (partition by address) mod9
    from ifstatus
    where ifadminstatus = '1' and ifoperstatus = '1';
0

精彩评论

暂无评论...
验证码 换一张
取 消