0

在此帖子的基础上构建

如何在 SQL Server 2019 中实现这一点?我有一个订单表,其中有一lineaggregate列包含一个 json 字符串,其中包含已发货的行项目详细信息。我需要将每个变体 ID 和数量提取到单独的行中(每个变体 ID 1 行)。

下面是一个实际的 json 字符串示例:

[
  {
    "id": 4379711799354,
    "variant_id": 31291380727866,
    "title": "*NEW* Kayslin Sneaker in Blush",
    "quantity": 1,
    "sku": "79000212",
    "variant_title": "3Y",
    "vendor": "TESTVENDOR",
    "fulfillment_service": "manual",
    "product_id": 4371426607162,
    "requires_shipping": true,
    "taxable": true,
    "gift_card": false,
    "name": "*NEW* Kayslin Sneaker in Blush - 3Y",
    "variant_inventory_management": "shopify",
    "properties": [
      {
        "name": "Item Ships Separately",
        "value": "✓"
      }
    ],
    "product_exists": true,
    "fulfillable_quantity": 0,
    "grams": 490,
    "price": "68.00",
    "total_discount": "0.00",
    "fulfillment_status": "fulfilled",
    "pre_tax_price": "68.00",
    "price_set": {
      "shop_money": {
        "amount": "68.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "68.00",
        "currency_code": "USD"
      }
    },
    "pre_tax_price_set": {
      "shop_money": {
        "amount": "68.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "68.00",
        "currency_code": "USD"
      }
    },
    "total_discount_set": {
      "shop_money": {
        "amount": "0.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "0.00",
        "currency_code": "USD"
      }
    },
    "discount_allocations": [],
    "admin_graphql_api_id": "gid:\/\/shopify\/LineItem\/4379711799354",
    "tax_lines": [
      {
        "title": "CT STATE TAX",
        "price": "0.00",
        "rate": 0.0,
        "price_set": {
          "shop_money": {
            "amount": "0.00",
            "currency_code": "USD"
          },
          "presentment_money": {
            "amount": "0.00",
            "currency_code": "USD"
          }
        }
      }
    ],
    "origin_location": {
      "id": 624876322874,
      "country_code": "US",
      "province_code": "TX",
      "name": "Joyfolie",
      "address1": "1809 W Frankford Rd #160 ",
      "address2": "",
      "city": "Carrollton",
      "zip": "75007"
    }
  },
  {
    "id": 4379711832122,
    "variant_id": 31031946838074,
    "title": "*NEW* Mama Bird Necklace Set in Gold",
    "quantity": 1,
    "sku": "83000109",
    "variant_title": "",
    "vendor": "TESTVENDOR",
    "fulfillment_service": "manual",
    "product_id": 4320751878202,
    "requires_shipping": true,
    "taxable": true,
    "gift_card": false,
    "name": "*NEW* Mama Bird Necklace Set in Gold",
    "variant_inventory_management": "shopify",
    "properties": [],
    "product_exists": true,
    "fulfillable_quantity": 0,
    "grams": 113,
    "price": "29.50",
    "total_discount": "0.00",
    "fulfillment_status": "fulfilled",
    "pre_tax_price": "29.50",
    "price_set": {
      "shop_money": {
        "amount": "29.50",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "29.50",
        "currency_code": "USD"
      }
    },
    "pre_tax_price_set": {
      "shop_money": {
        "amount": "29.50",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "29.50",
        "currency_code": "USD"
      }
    },
    "total_discount_set": {
      "shop_money": {
        "amount": "0.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "0.00",
        "currency_code": "USD"
      }
    },
    "discount_allocations": [],
    "admin_graphql_api_id": "gid:\/\/shopify\/LineItem\/4379711832122",
    "tax_lines": [
      {
        "title": "CT STATE TAX",
        "price": "0.00",
        "rate": 0.0,
        "price_set": {
          "shop_money": {
            "amount": "0.00",
            "currency_code": "USD"
          },
          "presentment_money": {
            "amount": 
4

1 回答 1

0

这不是一个有效的 JSON,但如果我们安排以使其有效,我们会得到以下结果:

{
  "Products": [ 
  {
    "id": 4379711799354,
    "variant_id": 31291380727866,
    "title": "*NEW* Kayslin Sneaker in Blush",
    "quantity": 1,
    "sku": "79000212",
    "variant_title": "3Y",
    "vendor": "TESTVENDOR",
    "fulfillment_service": "manual",
    "product_id": 4371426607162,
    "requires_shipping": true,
    "taxable": true,
    "gift_card": false,
    "name": "*NEW* Kayslin Sneaker in Blush - 3Y",
    "variant_inventory_management": "shopify",
    "properties": [
      {
        "name": "Item Ships Separately",
        "value": "✓"
      }
    ]
    }
,
  {
    "id": 4379711832122,
    "variant_id": 31031946838074,
    "title": "*NEW* Mama Bird Necklace Set in Gold",
    "quantity": 1,
    "sku": "83000109",
    "variant_title": "",
    "vendor": "TESTVENDOR",
    "fulfillment_service": "manual",
    "product_id": 4320751878202,
    "requires_shipping": true,
    "taxable": true,
    "gift_card": false,
    "name": "*NEW* Mama Bird Necklace Set in Gold",
    "variant_inventory_management": "shopify",
    "properties": [],
    "product_exists": true,
    "fulfillable_quantity": 0,
    "grams": 113,
    "price": "29.50",
    "total_discount": "0.00",
    "fulfillment_status": "fulfilled",
    "pre_tax_price": "29.50",
    "price_set": {
      "shop_money": {
        "amount": "29.50",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "29.50",
        "currency_code": "USD"
      }
    },
    "pre_tax_price_set": {
      "shop_money": {
        "amount": "29.50",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "29.50",
        "currency_code": "USD"
      }
    },
    "total_discount_set": {
      "shop_money": {
        "amount": "0.00",
        "currency_code": "USD"
      },
      "presentment_money": {
        "amount": "0.00",
        "currency_code": "USD"
      }
    } } ] }

OPENJSON()然后,您可以通过在查询中使用具有连接贡献的函数来提取元素CROSS APPLY

SELECT Q2.*
  FROM tab
 CROSS APPLY OPENJSON(JsonData,'$.Products')
             WITH (
                    Products nvarchar(max) '$' AS JSON
                    ) Q1
 CROSS APPLY OPENJSON (Q1.Products) 
             WITH (
                    ID         nvarchar(max) '$.id',
                    variant_id nvarchar(max) '$.variant_id',
                    quantity   nvarchar(max) '$.quantity',
                    properties nvarchar(max) '$.properties[0].name',
                    properties nvarchar(max) '$.properties[0].value',
                    price_shop_money_amt nvarchar(max) '$.price_set.shop_money.amount',
                    price_shop_money_cur nvarchar(max) '$.price_set.shop_money.currency_code'
                  ) Q2 

Demo

于 2020-08-14T07:03:43.143 回答