如何编写 SQL 代码以生成 4 级 JSON 输出?

问题描述

我正在尝试创建一个 4 级 json。搞砸我的是这里的“skus”级别。我正在尝试创建一个组级别,然后是一个 skus 级别、一个 fieldInfo 级别,并在其中创建给定组中所有 skus 的所有字段值等。

这是我的数据示例:

SKU 字段组 GroupSort 字段名称 FieldSort GroupBackgroundColor 标签名称 默认值
RB130-9-H-S21D2-PID-OS 合规性 9 item_volume_unit_of_measure 246 CC9999 item_volume_unit_of_measure
RB130-9-H-S21D2-PID-OS 产​​品丰富 6 leg_style 172 BBA680 腿型
RB130-9-H-S21D2-PID-OS 基础 4 apparel_size_class 56 F8A45E 服装尺码等级
RB130-9-H-S21D2-PID-OS 基础 4 shirt_size_class 51 F8A45E 衬衫尺码分类
RB130-9-H-S21D2-PID-OS 尺寸 7 item_height 198 8DB4E2 物品高度
RB130-9-H-S21D2-PID-OS 变化 3 parent_sku 43 FF8080 父 SKU
RB130-9-H-S21D2-SSKC-OS 变化 3 parent_sku 43 FF8080 父 SKU
RB130-9-H-S21D2-SSKC-OS 尺寸 7 item_height 198 8DB4E2 物品高度
RB130-9-H-S21D2-SSKC-OS 基础 4 shirt_size_class 51 F8A45E 衬衫尺码分类
RB130-9-H-S21D2-SSKC-OS 基础 4 apparel_size_class 56 F8A45E 服装尺码等级
RB130-9-H-S21D2-SSKC-OS 产​​品丰富 6 leg_style 172 BBA680 腿型
RB130-9-H-S21D2-SSKC-OS 合规 9 item_volume_unit_of_measure 246 CC9999 item_volume_unit_of_measure

我想要完成的事情是这样的:

{
    "groups": [{
        "group": "Group1","skus": [{
            "sku": "xxx","fieldInfo": [{
                "fieldName": "field1","value": "1"
            },{
                "fieldName": "2","fieldValue": "2"
            }]
        },{
            "sku": "yyy","fieldValue": "2"
            }]
        }]
    },{
        "group": "Group2",{
        "group": "Group3","fieldValue": "2"
            }]
        }]
    }]
}

到目前为止我的代码:

select (

select fieldGroup,max(groupbackgroundcolor) backgroundcolor,JSON_Query(max(skuinfo.skus)) skuinfo
    from Amazon.AmazonSpreadsheetData sd
    cross apply (
            select (select sd2.sku,JSON_Query(max(fieldinfo.fields)) fieldInfo from Amazon.AmazonSpreadsheetData sd2
                cross apply (
                        select (select fieldid,FieldName,LabelName,DefaultValue 
                        from Amazon.AmazonSpreadsheetData sd3
                        where sd3.sku=sd2.sku
                            and sd3.fieldgroup=sd2.fieldgroup
                            and sd3.collectionid=sd2.collectionid
                            and sd3.deliveryDate=sd2.deliverydate
                            and sd3. TemplateName=sd2.templatename
                            and sd3.productType=sd2.producttype
                        order by sd3.parentchildsort,sd3.fieldSort for JSON path ) fields               
                ) fieldInfo
            where sd2.fieldgroup=sd.fieldgroup 
                and sd2.sku=sd.sku 
                and sd2.collectionid=sd.collectionid
                and sd2.deliveryDate=sd.deliverydate
                and sd2.TemplateName=sd.templatename
                and sd2.productType=sd.producttype
            group by sd2.sku
            order by max(sd2.parentchildsort),sd2.sku 
            for JSON path )skus
            --order by parentchildsort,sd.sku for JSON path
            ) skuInfo

    where collectionid=524
        and sd.deliveryDate='2021-02-15'
        and sd.TemplateName='Boys'
        and sd.productType='pants'
        and sd.sku like '%RB130-9-%'
    group by fieldgroup
    --order by max(groupsort)
    for JSON path,root('Groups') 
) jstring 

但所做的只是获取每个组的最大 sku:

{
"Groups": [{
    "fieldGroup": "Basic","backgroundcolor": "F8A45E","skuinfo": [{
        "sku": "RB130-9-H-S21D2-SSKC-OS","fieldInfo": [{
            "fieldid": 220702,"FieldName": "update_delete","LabelName": "Update Delete","DefaultValue": "Update"
        },{
            "fieldid": 221206,"FieldName": "product_description","LabelName": "Product Description","DefaultValue": "descrip"
        }]
    }]
},{
    "fieldGroup": "Compliance","backgroundcolor": "CC9999","fieldInfo": [{
            "fieldid": 221390,"FieldName": "fabric_type","LabelName": "Fabric Type","DefaultValue": ""
        },{
            "fieldid": 220742,"FieldName": "import_designation","LabelName": "Import Designation","DefaultValue": ""
        }]
    }]
},{
    "fieldGroup": "Dimensions","backgroundcolor": "8DB4E2","fieldInfo": [{
            "fieldid": 221478,"FieldName": "chest_size","LabelName": "Chest Size",{
            "fieldid": 220930,"FieldName": "chest_size_unit_of_measure","LabelName": "Chest Size Unit Of Measure",{
    "fieldGroup": "Discovery","backgroundcolor": "92D050","fieldInfo": [{
            "fieldid": 220842,"FieldName": "bullet_point1","LabelName": "Key Product Features",{
            "fieldid": 221502,"FieldName": "bullet_point2",{
    "fieldGroup": "Fulfillment","backgroundcolor": "B7DEE8","fieldInfo": [{
            "fieldid": 221190,"FieldName": "fulfillment_center_id","LabelName": "Fulfillment Center ID","DefaultValue": "DEFAULT"
        },{
            "fieldid": 221422,"FieldName": "package_height","LabelName": "Package Height",{
    "fieldGroup": "Images","backgroundcolor": "FFFF00","fieldInfo": [{
            "fieldid": 221358,"FieldName": "other_image_url1","LabelName": "Other Image URL1",{
            "fieldid": 220858,"FieldName": "other_image_url2","LabelName": "Other Image URL2",{
    "fieldGroup": "Offer","backgroundcolor": "FF0000","fieldInfo": [{
            "fieldid": 221014,"FieldName": "list_price","LabelName": "Manufacturer's Suggested Retail Price",{
            "fieldid": 221414,"FieldName": "map_price","LabelName": "Minimum Advertised Price",{
    "fieldGroup": "Product Enrichment","backgroundcolor": "BBA680","fieldInfo": [{
            "fieldid": 221398,"FieldName": "bottom_style","LabelName": "Bottom Style",{
            "fieldid": 220798,"FieldName": "subject_character","LabelName": "character",{
    "fieldGroup": "Required","backgroundcolor": "FCD5B4","fieldInfo": [{
            "fieldid": 221418,"FieldName": "feed_product_type","LabelName": "Product Type","DefaultValue": "pants"
        },{
            "fieldid": 220758,"FieldName": "item_sku","LabelName": "Seller SKU","DefaultValue": "KP-RB130-9-S21D2"
        }]
    }]
},{
    "fieldGroup": "Variation","backgroundcolor": "FF8080","fieldInfo": [{
            "fieldid": 220934,"FieldName": "parent_child","LabelName": "Parentage","DefaultValue": "Parent"
        },{
            "fieldid": 221538,"FieldName": "parent_sku","LabelName": "Parent SKU",{
            "fieldid": 221234,"FieldName": "relationship_type","LabelName": "Relationship Type","DefaultValue": "Variation"
        },{
            "fieldid": 221030,"FieldName": "variation_theme","LabelName": "Variation Theme","DefaultValue": "SizeColor"
        }]
    }]
}]
}

我错过了一个步骤或一个组或其他东西。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...