postgresql的嵌套array_to_json输出

时间:2016-12-16 16:33:40

标签: postgresql

我编写了一个PSQL语句,它在JSON中返回多个表的数据,输出几乎是正确的,但是我需要更深层次,我不知道该怎么做。

声明如下:

[  
   {  
      "name":"Boston",
      "signatures":[  
         {  
            "eventid":1,
            "sensorid":1,
            "signature":223123,
            "srcip":"10.0.0.1",
            "dstip":"10.0.0.2",
            "srcport":10000,
            "dstport":80,
            "timestamp":"2016-12-10T07:00:00",
            "gid":null
         },
         {  
            "eventid":2,
            "sensorid":1,
            "signature":142142,
            "srcip":"10.0.0.1",
            "dstip":"10.0.0.2",
            "srcport":10000,
            "dstport":80,
            "timestamp":"2016-12-10T07:00:00",
            "gid":null
         }
      ]
   },
   {  
      "name":"Greenwich",
      "signatures":[  
         {  
            "eventid":12,
            "sensorid":2,
            "signature":123123,
            "srcip":"10.0.0.1",
            "dstip":"10.0.0.2",
            "srcport":10000,
            "dstport":80,
            "timestamp":"2016-12-10T07:00:00",
            "gid":null
         },
         {  
            "eventid":13,
            "sensorid":2,
            "signature":524123,
            "srcip":"10.0.0.1",
            "dstip":"10.0.0.2",
            "srcport":10000,
            "dstport":80,
            "timestamp":"2016-12-10T07:00:00",
            "gid":null
         }
      ]
   }
]

其中输出以下内容:

   {  
      "name":"Boston",
      "signatures":[  
         {  
            "signature": 223123,
            "firings":[
               {
                  "eventid":1,
                  "sensorid":1,
                  "signature":223123,
                  "srcip":"10.0.0.1",
                  "dstip":"10.0.0.2",
                  "srcport":10000,
                  "dstport":80,
                  "timestamp":"2016-12-10T07:00:00",
                  "gid":null
               }
            ]
         },
         {  
            "signature": 142142,
            "firings":[
               {
                  "eventid":1,
                  "sensorid":1,
                  "signature":142142,
                  "srcip":"10.0.0.1",
                  "dstip":"10.0.0.2",
                  "srcport":10000,
                  "dstport":80,
                  "timestamp":"2016-12-10T07:00:00",
                  "gid":null
               }
            ]
         }
      ]
   },

这是一个好的开始,但我还想要进一步对输出进行分层,以便各个签名成为一个包含所有内容的数组,因此输出应该是这样的:

select array_to_json(array_agg(t))
from (
      select sensors.name,
      (
      select array_to_json(array_agg(row_to_json(d)))
      from (
        select distinct events.signature as signatureid,
        (
        select array_to_json(array_agg(row_to_json(e)))
        from (
          select * 
          from events
          where  events.signature = signatureid
        ) e
        ) as firings
        from events
        where sensors.sensorid=events.sensorid
      ) d
    ) as signatures
  from sensors
) t

我试图这样做:

CONFIG += static

但是postgres似乎并不喜欢这样的说法"也许你的意思是参考专栏" events.signature"或列" events.signature"。"

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

i