我想进行查询以给出结果,如果有重复的ID,它将被分组,并且下面的数据将完整地保留到相同的分组数据中,并且全部在数组JSON数据中。
我这样查询:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\pygame\_camera_vidcapture.py", line 31, in init
import vidcap as vc
ModuleNotFoundError: No module named 'vidcap'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pygame.camera.init()
File "C:\Program Files\Python37\lib\site-packages\pygame\camera.py", line 69, in init
_camera_vidcapture.init()
File "C:\Program Files\Python37\lib\site-packages\pygame\_camera_vidcapture.py", line 33, in init
from VideoCapture import vidcap as vc
ModuleNotFoundError: No module named 'VideoCapture'
结果如下。
SELECT json_build_object(
'nama_perusahaan',"a"."nama_perusahaan",
'proyek', 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"
我期望的是这样。
{
"nama_perusahaan" : "JASA FERRIE",
"proyek" :
{
"no_izin" : "26A/E/IU/PMA/D8FD",
"kode" : "14302",
"judul_kode" : "IND"
}
}
{
"nama_perusahaan" : "JASA FERRIE",
"proyek" :
{
"no_izin" : "26A/E/IU/PMA/D8FD",
"kode" : "13121",
"judul_kode" : "IND B"
}
}
我该如何进行查询?
答案 0 :(得分:1)
您需要打开聚合,并使用json_agg()
生成正确的数据结构。
这应该接近您想要的:
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"