如何在Postgresql中按嵌套数组的重复值分组?

时间:2019-11-01 04:32:30

标签: sql json postgresql

先前的问题:How to group by duplicate value and nested the array Postgresql

使用此查询:

# break short into steps 
# 1. replace
df2 = df2.replace({'Rqd': 'REQD', 'Opt': 'OPT'})

# 2. pivot
df2 = df2.pivot(*df2.columns)

# 3. reindex
df2 = df2.reindex_like(df1)

# 4. fillna(cleanup df with string form)
df2 = df2.fillna('')

# 5. map on df1 and add up with df2
df1=df1.replace({'Y':'Y_'})+df2

结果如下所示:

    SELECT json_build_object(
    'nama_perusahaan',"a"."nama_perusahaan",
    'proyek', json_agg(
            json_build_object(
            'no_izin',"b"."no_izin",
            'kode',c.kode,
            'judul_kode',d.judul
        )
    )
)
FROM "t_pencabutan" "a"
LEFT JOIN "t_pencabutan_non" "b" ON "a"."id_pencabutan" = "b"."id_pencabutan"
LEFT JOIN "t_pencabutan_non_b" "c" ON "b"."no_izin" = "c"."no_izin"
LEFT JOIN "t_pencabutan_non_c" "d" ON "c"."id_proyek" = "d"."id_proyek"
GROUP BY "a"."nama_perusahaan"

如您所见,{ "nama_perusahaan" : "JASA FERRIE", "proyek" : { "no_izin" : "26A/E/IU/PMA/D8FD", "kode" : "14302", "judul_kode" : "IND" } { "no_izin" : "26A/E/IU/PMA/D8FD", "kode" : "13121", "judul_kode" : "IND B" } } 已嵌套,因此重复的proyek将被分组。现在,我必须将proyek的相同值分组,这样它将像下面的预期结果一样将嵌套数组加倍。

no_izin

我尝试使用此查询:

{
    "nama_perusahaan" : "JASA FERRIE", 
    "proyek" : 
    [{
        "no_izin" : "26A/E/IU/PMA/D8FD", 
        "kode_list":[
         {
              "kode" : "14302", 
              "judul_kode" : "IND"
         },
         {
              "kode" : "13121", 
              "judul_kode" : "IND B"
         }]   
    }]
}

但是它没有用,它给出了SELECT json_build_object( 'nama_perusahaan',"a"."nama_perusahaan", 'proyek', json_agg( json_build_object( 'no_izin',"b"."no_izin", 'kode_list',json_agg( json_build_object( 'kode',c.kode, 'judul_kode',d.judul ) ) ) ) ) FROM "t_pencabutan" "a" LEFT JOIN "t_pencabutan_non" "b" ON "a"."id_pencabutan" = "b"."id_pencabutan" LEFT JOIN "t_pencabutan_non_b" "c" ON "b"."no_izin" = "c"."no_izin" LEFT JOIN "t_pencabutan_non_c" "d" ON "c"."id_proyek" = "d"."id_proyek" GROUP BY "a"."nama_perusahaan", b.no_izin 。 我的代码可能出什么问题了?

1 个答案:

答案 0 :(得分:1)

免责声明:对于我们来说,在不了解输入数据和表结构且必须处理未知语言的情况下构造查询非常困难。请尝试最小化您的其他问题(例如,对于您的问题,在将结果转换为JSON输出之前不需要先连接一些表并不重要),用英语创建示例(处理外语会使代码看起来混乱,并导致拼写错误,因此可能正确的想法无法将错误的单词写成错误),然后添加输入数据!这也将对您有帮助:您将更快地得到答案,并且代码错误的可能性要少得多(因为现在如果没有数据,我们将无法创建可运行的示例来检查我们的想法)。


只有从最内层的嵌套对象到最外层的嵌套对象,才能创建嵌套的JSON结构。因此,首先您必须在子查询中创建no_izin数组。这可以用来创建proyek对象:

SELECT 
    json_build_object(
        'nama_perusahaan',"s"."nama_perusahaan",
        'proyek', json_agg(no_izin)
    )
)
FROM (
    SELECT
        "a"."nama_perusahaan",
        json_build_object(
            'no_izin',
            "b"."no_izin",
            'kode_list',
            json_agg(
                json_build_object(
                    'kode',c.kode,
                     'judul_kode',d.judul
                )
            )
        ) AS no_izin
    FROM "t_pencabutan" "a"
    LEFT JOIN "t_pencabutan_non" "b" ON "a"."id_pencabutan" = "b"."id_pencabutan"
    LEFT JOIN "t_pencabutan_non_b" "c" ON "b"."no_izin" = "c"."no_izin"
    LEFT JOIN "t_pencabutan_non_c" "d" ON "c"."id_proyek" = "d"."id_proyek"
    GROUP BY "c"."id_proyek", "a"."nama_perusahaan"
) AS s
GROUP BY "s"."nama_perusahaan"