如何在输入中返回数组元素的值,该元素所在的索引不断变化?
我非常确定我的查询结构是正确的。我有两个输入,正在使用连接,并成功地从两个表中获取了一些数据。但是,我需要从表B中获取RemoteIpAddress,但它在一个json格式的数组中。
如果您想要轻松地复制、粘贴和/或编辑它,可以在这里使用文本形式:
SELECT
A.context.data.eventTime as eventTime,
A.context.device.type as deviceType,
A.context.[user].anonId as userId,
A.context.device.roleInstance as machineName,
B.context.operation.name as eventName,
B.context.custom.dimensions[0],
--B.GetRecordPropertyValue(GetArrayElement(B.context.custom.dimensions,7), B.RemoteIpAddress) as remoteIpAddress,
--GetArrayElement(B.context.custom.dimensions,3),
--B.GetRecordPropertyValue(GetArrayElement(B.context.custom.dimensions,3), B.userName) as userName,
DATEDIFF(minute,A.context.data.eventTime,B.context.data.eventTime) as durationInMinutes
INTO DevUserlgnsOutput
FROM DevUserlgnsInput A TIMESTAMP BY A.context.data.eventTime
JOIN DevUserlgnsInput2 B TIMESTAMP BY B.context.data.eventTime
ON DATEDIFF(minute,A,B) BETWEEN 0 AND 5
注释掉的行不起作用,所以我把它们注释掉了。
我查了一下,看到了使用GetRecordPropertyValue和GetArrayElement的建议,所以我就这么做了。我没有收到任何错误,但它返回null。
我还发现,如果我执行B.context.custom.dimensions,将返回包含我想要查看的元素的完整数组。
更复杂的是,我意识到我想要的元素在数组中的位置并不总是相同的。在一些示例数据中,它是7,而在其他示例数据中,它是3。
提前谢谢。
阅读答案后更新:
我的新问题:
SELECT
Events.context.data.eventTime as eventTime,
Events.context.device.type as deviceType,
mDim.ArrayValue.MachineName as machineName,
mDim.ArrayValue.UserId as userID,
mDim.ArrayValue.RemoteIpAddress as remoteIpAddress,
mDim.ArrayValue.UserName as userName,
mDim.ArrayValue.EventName as eventName
INTO DevUserlgnsOutput
FROM DevUserlgnsInput2 Events
CROSS APPLY GetArrayElements(Events.context.custom.dimensions) AS mDim
问题:我现在对单个事件有多个行,每行显示一个我想要跟踪的属性(每行中与数组相关的其余列为NULL)。你有什么想法来解决这个问题吗?
发布于 2018-06-06 22:43:18
我的解决方案:
WITH Events AS
(
SELECT
context.data.EventTime as eventTime,
context.device.type as deviceType,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 7), 'MachineName') AS machineName,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 8), 'UserName') AS userName,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 2), 'remoteIpAddress') AS remoteIpAddress,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 0), 'EventName') AS eventName,
CASE WHEN GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 12), 'UserId') is NULL THEN GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 11), 'UserId') ELSE GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 12), 'UserId') END as userId
FROM ProdUserlgnsInput
)
SELECT eventTime, deviceType, MachineName, UserId, UserName, remoteIpAddress, eventName INTO ProdUserlgnsOutput FROM Events
但是,我必须将EventName属性移到主数组中,因为我试图使用WITH语句从两个独立的数组中获取信息,但它不允许我将结果放在单个输出中。此外,由于UserId的索引大部分是12,但有时是11。因此,为了显示所有记录的实际UserId,我使用了"Case When“语法。
我做了很多工作来解决这个问题,所以如果有人想要更多的细节,请随时询问。
发布于 2018-06-15 04:23:23
下面的查询符合您最新的数组结构,请尝试一下:
SELECT
context.data.EventTime as eventTime,
context.device.type as deviceType,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 4), 'MachineName') AS machineName,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 5), 'UserId') AS userId,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 9), 'UserName') AS userName,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 11), 'remoteIpAddress') AS remoteIpAddress,
GetRecordPropertyValue(GetArrayElement(context.custom.dimensions, 13), 'EventName') AS eventName
INTO output1
FROM input1
发布于 2019-05-20 22:16:17
您可以使用UDF
function arraygetvaluebyname(arg, name) {
var z = arg;
for(var i=0;i<z.length;i++){
if(name === Object.keys(z[i])[0])
{
return z[i][name];
}
}
return null;
}
https://stackoverflow.com/questions/50680891
复制相似问题