Postgres - 以 JSON 数组或数组的形式返回行数据

zam*_*6ak 5 sql arrays postgresql json postgresql-9.4

在 PG v9.4 及更高版本上,我想将一些数据(基于 SELECT 语句)导出为JSON 数组

标准 json_agg 返回我想要的内容,但它返回对象数组(其中对象键是列名称)例如此查询:

SELECT json_agg(data_rows)
FROM (
        -- in memory table sample
    SELECT * FROM 
    (VALUES
        ('John',now(),1940,'Winston','Lennon'),
        ('Paul',now(),1942,'','McCartney'),
        ('George',now(),1943,NULL,'Harrison'),
        ('Ringo',now(),1940,'my passions are ring,drum and shades','Starr')
    ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName")
        ORDER BY "BirthYear" DESC, "FirstName" DESC
) AS data_rows
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

[
 {"FirstName":"George","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1943,"MiddleName":null,"LastName":"Harrison"}, 
 {"FirstName":"Paul","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1942,"MiddleName":"","LastName":"McCartney"}, 
 {"FirstName":"Ringo","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1940,"MiddleName":"my passions are ring,drum and shades","LastName":"Starr"}, 
 {"FirstName":"John","CurrentDt":"2016-09-12T13:13:07.862485-04:00","BirthYear":1940,"MiddleName":"Winston","LastName":"Lennon"}
]
Run Code Online (Sandbox Code Playgroud)

但我想要的是:

[
 ["George","2016-09-12T13:13:07.862485-04:00",1943,null,"Harrison"}, 
 ["Paul","2016-09-12T13:13:07.862485-04:00",1942,"","McCartney"}, 
 ["Ringo","2016-09-12T13:13:07.862485-04:00",1940,"my passions are ring,drum and shades","Starr"}, 
 ["John","2016-09-12T13:13:07.862485-04:00",1940,"Winston","Lennon"}
]
Run Code Online (Sandbox Code Playgroud)

我尝试使用这里提到的技巧首先将行转换为 hstore,但这样做的问题是没有保留列排序......所以这个查询:

SELECT json_agg(avals(hstore(data_rows)))
FROM (
        -- in memory table sample
    SELECT * FROM 
    (VALUES
        ('John',  now(),1940,'Winston','Lennon'),
        ('Paul',  now(),1942,'','McCartney'),
        ('George',now(),1943,NULL,'Harrison'),
        ('Ringo', now(),1940,'my passions are ring,drum and shades','Starr')
    ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName")
        ORDER BY "BirthYear" DESC, "FirstName" DESC
) AS data_rows
-- placing order by 'outside' did not make any difference
--ORDER BY "BirthYear" DESC, "FirstName" DESC
Run Code Online (Sandbox Code Playgroud)

返回以下内容(列顺序错误)

[
 ["Harrison","1943","2016-09-12 14:07:06.772227-04","George",null], 
 ["McCartney","1942","2016-09-12 14:07:06.772227-04","Paul",""], 
 ["Starr","1940","2016-09-12 14:07:06.772227-04","Ringo","my passions are ring,drum and shades"], 
 ["Lennon","1940","2016-09-12 14:07:06.772227-04","John","Winston"]
]
Run Code Online (Sandbox Code Playgroud)

有谁知道如何以 JSON 数组形式获取结果?

red*_*neb 6

你可能想要这样的东西:

SELECT json_agg(info)
FROM (
    SELECT json_build_array("LastName","BirthYear","CurrentDt","FirstName","MiddleName") AS info
    FROM 
        (VALUES
            ('John',now(),1940,'Winston','Lennon'),
            ('Paul',now(),1942,'','McCartney'),
            ('George',now(),1943,NULL,'Harrison'),
            ('Ringo',now(),1940,'my passions are ring,drum and shades','Starr')
        ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName")
    ORDER BY "BirthYear" DESC, "FirstName" DESC
) as t;
Run Code Online (Sandbox Code Playgroud)

我曾经json_build_array将每个人的所有值放入一个 json 数组中,然后在外部查询中,我曾经json_agg将所有这些数组收集到单个数组中。

您还可以将ORDER BY子句移动到聚合函数内以获得以下内容:

SELECT json_agg(json_build_array("LastName","BirthYear","CurrentDt","FirstName","MiddleName") ORDER BY "BirthYear" DESC, "FirstName" DESC)
    FROM 
        (VALUES
            ('John',now(),1940,'Winston','Lennon'),
            ('Paul',now(),1942,'','McCartney'),
            ('George',now(),1943,NULL,'Harrison'),
            ('Ringo',now(),1940,'my passions are ring,drum and shades','Starr')
        ) AS x("FirstName", "CurrentDt", "BirthYear", "MiddleName", "LastName");
Run Code Online (Sandbox Code Playgroud)