如何在json格式的列中计算键值对

时间:2019-06-18 14:53:34

标签: json sql-server

我在要查询的列(“数据”)中具有以下JSON格式的数据: 记录1:

{  
    "actions":[  
        {  
            "class":"Action",
            "entries":[  
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartTime",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560858977855,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"O",
                    "value":"\"2019-06-17T11:58:38.000Z\""
                },
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartedBy",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560858977855,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"C_R",
                    "value":"1019"
                }
            ]
        }
    ]
}

记录2:

{  
    "actions":[  
        {  
            "class":"Action",
            "entries":[  
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartTime",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560863293329,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"O",
                    "value":null
                },
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StopTime",
                    "performance":{  
                        "class":"com.bma.common.dto.Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560863293329,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"O",
                    "value":"\"2019-06-18T13:08:42.000Z\""
                },
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartedBy",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560858977855,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e6"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"C_R",
                    "value":"1018"
                }
            ]
        }
    ]
}

现在我需要确定的是每个开始时间是否都有停止时间,以便确定是否还有任何正在进行的操作。请注意,第一个记录没有带有StopTime键的对象。也有没有StartTime键的记录。并且,请参见记录2,带有StartTime键的对象可以具有空值。具有StopTime键的对象也是如此。我炮制了以下尚未完成的sql:

SELECT ca1.[value] AS StartTimeCol,
ca2.[value] AS StopTimeCol
FROM queriedTable qu
CROSS APPLY OPENJSON(qu.Data, '$.actions')
WITH (
entries nvarchar(max) '$.entries' AS JSON
) j
CROSS APPLY OPENJSON(j.entries)
WITH (
[key] nvarchar(100) '$.key',
[value] nvarchar(100) '$.value'
) ca1
CROSS APPLY OPENJSON(j.entries)
WITH (
[key] nvarchar(100) '$.key',
[value] nvarchar(100) '$.value'
) ca2
WHERE
ca1.[key] = 'StartTime' OR
ca2.[key] = 'StopTime'

很显然,我已经开始计算StartTimes和StopTimes了;让我感到困惑的是,尽管我明确指出仅必须考虑键StartTime和StopTime,但结果却在其他对象(例如“ 1019”和“ 1018”)的StartTimeCol和StopTimeCol列中给出了我的值。怎么来的?并且如何在一个查询中确定StopTimes的数量是否大于或等于StartTimes的数量?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么下一种方法可能会有所帮助:

表格:

CREATE TABLE queriedTable (
   Data nvarchar(max)
)
INSERT INTO queriedTable 
   (Data)
VALUES
    (N'{  
    "actions":[  
        {  
            "class":"Action",
            "entries":[  
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartTime",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560858977855,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"O",
                    "value":"\"2019-06-17T11:58:38.000Z\""
                },
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartedBy",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560858977855,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"C_R",
                    "value":"1019"
                }
            ]
        }
    ]
}'
   ),
   (N'{  
    "actions":[  
        {  
            "class":"Action",
            "entries":[  
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartTime",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560863293329,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"O",
                    "value":null
                },
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StopTime",
                    "performance":{  
                        "class":"com.bma.common.dto.Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560863293329,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e7"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"O",
                    "value":"\"2019-06-18T13:08:42.000Z\""
                },
                {  
                    "class":"Entry",
                    "id":null,
                    "key":"StartedBy",
                    "performance":{  
                        "class":"Performance",
                        "origin":null,
                        "originUuid":null,
                        "performanceDateTime":1560858977855,
                        "performerUuid":{  
                            "class":"java.util.UUID",
                            "UUID":"2647a005-a3a8-4362-8f2d-ddd188f500e6"
                        },
                        "registrationDateTime":null,
                        "userUuid":null
                    },
                    "type":"C_R",
                    "value":"1018"
                }
            ]
        }
    ]
}')

T-SQL:

SELECT
   RANK() OVER (ORDER BY qu.Data) Rn,
   COUNT(*) OVER (PARTITION BY qu.Data) [Count],
   b.*
FROM queriedTable qu
CROSS APPLY OPENJSON(qu.Data, '$.actions') WITH (
   Data nvarchar(max) '$.entries' AS JSON
) a
CROSS APPLY OPENJSON(a.Data)  WITH (
   [key] nvarchar(max) '$.key',
   [value] nvarchar(max) '$.value'
) b
WHERE b.[key] = 'StartTime' OR b.[key] = 'StopTime'

输出:

Rn  Count   key          value
1   1       StartTime   "2019-06-17T11:58:38.000Z"
2   2       StartTime   
2   2       StopTime    "2019-06-18T13:08:42.000Z"