SQL OPENJSON仅获取第一个元素

vql8enpb  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(145)

我尝试只从JSON字符串中的第一个元素获取数据,因为第一个元素总是最新的。我尝试了一些方法,但没有成功。也许这里有人能给我指出正确的方向。
下面是JSON字符串:

[
{
    "wd:Transaction_Log_Entry": [
        {
            "wd:Transaction_Log_Reference": {
                "@wd:Descriptor": "Rescind of End Contract: XXXX (Rescinded)",
                "wd:ID": {
                    "@wd:type": "WID",
                    "#text": "fb27bafef89b101b5bf865947b420000"
                }
            },
            "wd:Transaction_Log_Data": {
                "wd:Transaction_Log_Description": "Rescind of End Contract: XXXX (Rescinded)",
                "wd:Transaction_Effective_Moment": "2023-01-18T09:00:00+01:00",
                "wd:Transaction_Entry_Moment": "2023-01-19T10:49:00.868+01:00",
                "wd:Is_Rescind_Or_Rescinded": "1",
                "wd:Is_Correction_Or_Corrected": "0"
            }
        },
        {
            "wd:Transaction_Log_Reference": {
                "@wd:Descriptor": "End Contract: XXXX (Rescinded)",
                "wd:ID": {
                    "@wd:type": "WID",
                    "#text": "a4a0fd2c2df8101bd1c6bde5f5710000"
                }
            },
            "wd:Transaction_Log_Data": {
                "wd:Transaction_Log_Description": "End Contract: XXXX (Rescinded)",
                "wd:Transaction_Effective_Moment": "2023-01-18T09:00:00+01:00",
                "wd:Transaction_Entry_Moment": "2023-01-18T12:41:43.867+01:00",
                "wd:Is_Rescind_Or_Rescinded": "1",
                "wd:Is_Correction_Or_Corrected": "0"
            }
        }
    ]
}
]

我的SQL查询-我从列名为“Transaction_Log_Entry_Data”的表中选择JSON字符串

SELECT Effective_Moment, Entry_Moment  
 FROM [dbo].[Daily_And_Future_Terminations_Transaction_Log_Source] 
 CROSS APPLY OPENJSON (Transaction_Log_Entry_Data, '$[0]."wd:Transaction_Log_Entry"')
 WITH (
        Effective_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Effective_Moment"',
        Entry_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Entry_Moment"'
       )

我的结果是2行,我只需要第一个元素中的数据:

最好的问候Ole

wqnecbli

wqnecbli1#

数组中有一个数组,所以必须写入。
JSOn是一种有趣的数据结构,但您需要大量的经验

SELECT Effective_Moment, Entry_Moment  
 FROM [Daily_And_Future_Terminations_Transaction_Log_Source] 
 CROSS APPLY OPENJSON (Transaction_Log_Entry_Data, '$[0]."wd:Transaction_Log_Entry"[0]')
 WITH (
        Effective_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Effective_Moment"',
        Entry_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Entry_Moment"'
       )

| 有效_力矩|进入_时刻|
| - ------|- ------|
| 2023年1月18日星期三09:00:00 + 01:00|2023年1月19日上午10时49分00秒868 + 01分00秒|
fiddle

SELECT TOP 1 Effective_Moment, Entry_Moment  
 FROM [Daily_And_Future_Terminations_Transaction_Log_Source] 
 CROSS APPLY OPENJSON (Transaction_Log_Entry_Data, '$[0]."wd:Transaction_Log_Entry"')
 WITH (
        Effective_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Effective_Moment"',
        Entry_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Entry_Moment"'
       )
ORDEr By Entry_Moment DESC

| 有效_力矩|进入_时刻|
| - ------|- ------|
| 2023年1月18日星期三09:00:00 + 01:00|2023年1月19日上午10时49分00秒868 + 01分00秒|
fiddle

相关问题