SQL:Finding SQL 服务器中嵌套 JSON 的最大值
SQL:Finding max value from a nested JSON in SQL Server
此问题在中有参考。
我想计算下面 JSON 的 MAX()
值。此外,其中的键不是固定的,因此根据上述解决方案,它们不能在代码中 硬编码 。
DECLARE @json nvarchar(max) = ''{"value": [
{
"AEDAT": "20211119",
"ERDAT": "20211119"
},
{
"AEDAT": "20211119",
"ERDAT": "20211112"
},
{
"AEDAT": "20211123",
"ERDAT": "20211123"
},
{
"AEDAT": "00000000",
"ERDAT": "20211119"
},
{
"AEDAT": "00000000",
"ERDAT": "20211123"
}]}';
此外,我将从其他一些 SP 获取以下字符串作为参数。
DECLARE @ColumnName = 'AEDAT,ERDAT' ---I shall be getting this as an output from another SP.
综上所述,我的做法是:
DECLARE @col_table TABLE (ID int IDENTITY(1,1), ColumnName varchar(100))
INSERT INTO @col_table SELECT value as ColumnName FROM STRING_SPLIT(@ColumnName,',');
DECLARE @max_val TABLE (ID int IDENTITY(1,1), LastUpdatedValue int)
DECLARE @max_ID int
DECLARE @min_ID int
DECLARE @1st_col varchar(max)
SELECT @max_ID = MAX(ID),@min_ID = MIN(ID) FROM @col_table GROUP BY ColumnName;
WHILE (@min_ID <= @max_ID)
BEGIN
SELECT @1st_col = ColumnName FROM @col_table WHERE ID = @min_ID
INSERT INTO @max_val (LastUpdatedValue)
SELECT JSON_VALUE(j.value,'$.' +@1st_col+ '' ) FROM OPENJSON(@json,'$.value') AS j
SET @min_ID = @min_ID + 1
END
理想情况下,上述方法应该给出 10 个值(AEDAT 和 ERDAT 各 5 个)但这给了我 5 个值。
SELECT * FORM @max_val;
ID LastUpdatedValue
1 20211119
2 20211112
3 20211123
4 20211119
5 20211123
我希望所有键的输出都像 UNION ALL
。喜欢下面
SELECT JSON_VALUE(j.value,'$.AEDAT') FROM OPENJSON(@json,'$.value') AS j
UNION ALL
SELECT JSON_VALUE(j.value,'$.ERDAT') FROM OPENJSON(@json,'$.value') AS j
我错过了什么?请提出建议。
您可以使用 OPENJSON
两次,一次用于打开数组,另一次用于反转键。然后只需按 table 变量
进行过滤
DECLARE @col_table TABLE (ColumnName varchar(100) COLLATE Latin1_General_BIN2 PRIMARY KEY)
INSERT INTO @col_table (ColumnName)
SELECT value
FROM STRING_SPLIT(@ColumnName,',');
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
j2.[key],
j2.value
FROM OPENJSON(@json, '$.value') j1
CROSS APPLY OPENJSON(j1.value) j2
WHERE CAST(j2.[key] AS varchar(100)) IN (
SELECT c.ColumnName
FROM @col_table c
);
此问题在
我想计算下面 JSON 的 MAX()
值。此外,其中的键不是固定的,因此根据上述解决方案,它们不能在代码中 硬编码 。
DECLARE @json nvarchar(max) = ''{"value": [
{
"AEDAT": "20211119",
"ERDAT": "20211119"
},
{
"AEDAT": "20211119",
"ERDAT": "20211112"
},
{
"AEDAT": "20211123",
"ERDAT": "20211123"
},
{
"AEDAT": "00000000",
"ERDAT": "20211119"
},
{
"AEDAT": "00000000",
"ERDAT": "20211123"
}]}';
此外,我将从其他一些 SP 获取以下字符串作为参数。
DECLARE @ColumnName = 'AEDAT,ERDAT' ---I shall be getting this as an output from another SP.
综上所述,我的做法是:
DECLARE @col_table TABLE (ID int IDENTITY(1,1), ColumnName varchar(100))
INSERT INTO @col_table SELECT value as ColumnName FROM STRING_SPLIT(@ColumnName,',');
DECLARE @max_val TABLE (ID int IDENTITY(1,1), LastUpdatedValue int)
DECLARE @max_ID int
DECLARE @min_ID int
DECLARE @1st_col varchar(max)
SELECT @max_ID = MAX(ID),@min_ID = MIN(ID) FROM @col_table GROUP BY ColumnName;
WHILE (@min_ID <= @max_ID)
BEGIN
SELECT @1st_col = ColumnName FROM @col_table WHERE ID = @min_ID
INSERT INTO @max_val (LastUpdatedValue)
SELECT JSON_VALUE(j.value,'$.' +@1st_col+ '' ) FROM OPENJSON(@json,'$.value') AS j
SET @min_ID = @min_ID + 1
END
理想情况下,上述方法应该给出 10 个值(AEDAT 和 ERDAT 各 5 个)但这给了我 5 个值。
SELECT * FORM @max_val;
ID LastUpdatedValue
1 20211119
2 20211112
3 20211123
4 20211119
5 20211123
我希望所有键的输出都像 UNION ALL
。喜欢下面
SELECT JSON_VALUE(j.value,'$.AEDAT') FROM OPENJSON(@json,'$.value') AS j
UNION ALL
SELECT JSON_VALUE(j.value,'$.ERDAT') FROM OPENJSON(@json,'$.value') AS j
我错过了什么?请提出建议。
您可以使用 OPENJSON
两次,一次用于打开数组,另一次用于反转键。然后只需按 table 变量
DECLARE @col_table TABLE (ColumnName varchar(100) COLLATE Latin1_General_BIN2 PRIMARY KEY)
INSERT INTO @col_table (ColumnName)
SELECT value
FROM STRING_SPLIT(@ColumnName,',');
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
j2.[key],
j2.value
FROM OPENJSON(@json, '$.value') j1
CROSS APPLY OPENJSON(j1.value) j2
WHERE CAST(j2.[key] AS varchar(100)) IN (
SELECT c.ColumnName
FROM @col_table c
);