我对sqljson特性还不熟悉
问题是:我想使用sqljson特性从下表数据生成所需的json结构。
我拥有的表格数据:
Col1 | Col2 | Col3 | Col4
--------------------------------
School | Room | Jon | Present
School | Room | Hanna |Absent
School | Room | Teena | NA
School | Hall | Length | 12
School | Hall | Breath | 11
School | Hall | Heught | 4
School | Ground | school | xuz
School | Ground | col | oo
School | Ground | else | a
College | ClassA | teacher| 2
College | ClassA | students|20
College | ClassA | others | 1
College | ClassB | Des | 3
College | ClassB | tv | 0
所需的json数据格式
{
"School":{
"Room":{
"Jon":"Present",
"Hanna":"Absent",
"Teena":"NA"
},
"Hall":{
"Length":"12",
"Breath":"11",
"Heught":"4"
},
"Ground":{
"school":"xuz",
"col":"oo",
"else":"a"
}
},
"College":{
"ClassA":{
"teacher":"2",
"students":"20",
"others":"1"
},
"ClassB":{
"Desk":"3",
"tv":"0"
}
}
}
我需要知道如何在上面给定的json格式的帮助下格式化数据 FOR JSON PATH
1条答案
按热度按时间nzkunb0c1#
对于postgres,您可以使用
jsonb_object_agg()
要实现这一点:在线示例