简体   繁体   English

SWQL/ SQL 分组并将一个字段中的多个结果合并为一个结果

[英]SWQL/ SQL group and combining multiple result in one field into single result

Is there any way to achieve such things in the expectation result below?有没有办法在下面的预期结果中实现这样的事情?

saw this example , but not sure to applied since there are many "JOIN" here.看到这个例子,但不确定应用,因为这里有很多“JOIN”。

Current original Statement:当前原始声明:

SELECT TOP 1000 n.NodeID,t.Name as tagName ,le.LogEntryID,  le.DateTime, MessageDateTime
FROM Orion.OLM.LogEntry le
JOIN Orion.OLM.LogEntryTagAssignment leta on leta.LogEntryID=le.LogEntryID
JOIN Orion.OLM.Tags t on t.LogEntryTagID=leta.LogEntryTagID
JOIN Orion.Nodes n on n.NodeID = le.NodeID

GROUP by t.Name, n.NodeID, le.DateTime

Result:结果:

NodeID  tagName         LogEntryID          DateTime                    MessageDateTime
210     ToBeDiscarded   1559852514889690000 2020-12-12T02:14:15.0530000 2020-12-12T10:14:15.0520000
210     Login Failure   1559852514889690000 2020-12-12T02:14:15.0530000 2020-12-12T10:14:15.0520000
210     MediaServer     1559852514889690000 2020-12-12T02:14:15.0530000 2020-12-12T10:14:15.0520000
210     RealTime        1559852514889690000 2020-12-12T02:14:15.0530000 2020-12-12T10:14:15.0520000
210     EscalateTo-L1   1560053462887030000 2020-12-12T05:33:52.4870000 2020-12-12T13:33:52.2710000
210     MediaServer     1560053462887030000 2020-12-12T05:33:52.4870000 2020-12-12T13:33:52.2710000
210     RealTime        1560053462887030000 2020-12-12T05:33:52.4870000 2020-12-12T13:33:52.2710000
210     EscalateTo-L2   1560053490082900000 2020-12-12T05:33:54.1070000 2020-12-12T13:33:54.1080000
210     MediaServer     1560053490082900000 2020-12-12T05:33:54.1070000 2020-12-12T13:33:54.1080000
210     RealTime        1560053490082900000 2020-12-12T05:33:54.1070000 2020-12-12T13:33:54.1080000

Expectation:期待:

NodeID  tagName                                                 LogEntryID          DateTime                    MessageDateTime
210     ToBeDiscarded, Login Failure, MediaServer, RealTime     1559852514889690000 2020-12-12T02:14:15.0530000 2020-12-12T10:14:15.0520000
210     EscalateTo-L1, MediaServer, RealTime                    1560053462887030000 2020-12-12T05:33:52.4870000 2020-12-12T13:33:52.2710000
210     EscalateTo-L2, MediaServer, RealTime                    1560053490082900000 2020-12-12T05:33:54.1070000 2020-12-12T13:33:54.1080000

Solarwinds is a suite of tools built (often) on top of SQL Server. Solarwinds 是(通常)在 SQL 服务器之上构建的一套工具。 SQL Server now offers string_agg() to do what you want: SQL 服务器现在提供string_agg()来做你想做的事:

SELECT n.NodeID, le.DateTime, 
       STRING_AGG(t.Name, ', ') as tagNames,
       le.LogEntryID, MessageDateTime
FROM Orion.OLM.LogEntry le JOIN
     Orion.OLM.LogEntryTagAssignment leta
     ON leta.LogEntryID=le.LogEntryID JOIN
     Orion.OLM.Tags t 
     ON t.LogEntryTagID=leta.LogEntryTagID JOIN
     Orion.Nodes n on n.NodeID = le.NodeID
GROUP by t.Name, n.NodeID, le.DateTime, e.LogEntryID;

This is possible in older versions of SQL Server but the syntax is more convoluted.这在旧版本的 SQL 服务器中是可能的,但语法更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM