编辑:
由于回复,我进一步清理了以下代码:
SELECT
AllAlerts.AlertID as AlertID,
Queues.QueueID as QueueID,
Queues.QueueName as QueueName,
AllAlerts.ConnectorID as ConnectorID,
cast( ISNULL(STUFF ( (
select cast(',' as varchar(max)) + Services.Label
from (
SELECT distinct Services.Label
from [ISG_SOI ].[dbo].[SecureServiceCI] as Services
inner join [ISG_SOI ].[dbo].[CIRelationship] as Relationship
on Relationship.BNodeCIID = AllAlerts.CIID
where Services.CIID = Relationship.ServiceCIID
) as Services
for xml path ('')
), 1, 1, ''), '') as CHAR(1000)) as OwnedServices,
right(AllAlerts.DeviceID, len(AllAlerts.DeviceID)-charindex(',', AllAlerts.DeviceID)) as CIName,
AllAlerts.DeviceID as DeviceID,
AllAlerts.SituationMessage as Summary,
AllAlerts.AlertDetail as Detail,
AllAlerts.Acknowledged as Acknowledged,
AllAlerts.AssignedTo as AssignedTo,
AllAlerts.ReportedTime as CreatedTime,
AllAlerts.ClearedTime as ClearedTime,
AllAlerts.Severity as Severity,
AllAlerts.SvcDeskTicket as TicketID,
ISNULL(STUFF ( (
select cast('# ' as varchar(max)) + Notes.AnnotationText + '[' + Notes.CreatedBy + ', ' + cast(Notes.CreatedTime as varchar(max)) + ']'
from [ISG_SOI ].[dbo].[AlertAnnotation] as Notes
where Notes.AlertID = AllAlerts.AlertID
for xml path('')
), 1, 1, ''), '') as Notes
from
[ISG_SOI ].[dbo].[Alerts] as AllAlerts
inner join [ISG_SOI ].[dbo].[AlertQueueAssignments] as QA
on QA.[AlertID] = AllAlerts.[AlertID]
inner join [ISG_SOI ].[dbo].[AlertQueues] AS Queues
on Queues.[QueueID] = QA.[QueueID]
where Queues.QueueName = 'OCC'
- 原始帖子 -
我一直在为我正在工作的项目进行T-SQL搜索,最后得到搜索参数以获取我需要的所有结果。我很好奇,有没有办法改进这个命令?你将不得不原谅我,因为我不是SQL专家。
SELECT AllAlerts.AlertID AS AlertID
,Queues.QueueID AS QueueID
,Queues.QueueName AS QueueName
,AllAlerts.ConnectorID AS ConnectorID
,CAST(ISNULL(STUFF(( SELECT CAST(',' AS VARCHAR(MAX)) + Services.Label
FROM ( SELECT DISTINCT Services.Label
FROM [ISG_SOI ].[dbo].[SecureServiceCI] AS Services
WHERE Services.CIID IN (
SELECT Relationship.ServiceCIID
FROM [ISG_SOI ].[dbo].[CIRelationship] AS Relationship
WHERE Relationship.BNodeCIID = AllAlerts.CIID ) ) AS Services
FOR
XML PATH('') ), 1, 1, ''), '') AS CHAR(1000)) AS OwnedServices
,RIGHT(AllAlerts.DeviceID, LEN(AllAlerts.DeviceID) - CHARINDEX(',', AllAlerts.DeviceID)) AS CIName
,AllAlerts.DeviceID AS DeviceID
,AllAlerts.SituationMessage AS Summary
,AllAlerts.AlertDetail AS Detail
,AllAlerts.Acknowledged AS Acknowledged
,AllAlerts.AssignedTo AS AssignedTo
,AllAlerts.ReportedTime AS CreatedTime
,AllAlerts.ClearedTime AS ClearedTime
,AllAlerts.Severity AS Severity
,AllAlerts.SvcDeskTicket AS TicketID
,ISNULL(STUFF(( SELECT CAST('# ' AS VARCHAR(MAX)) + Notes.AnnotationText + '[' + Notes.CreatedBy + ', '
+ CAST(Notes.CreatedTime AS VARCHAR(MAX)) + ']'
FROM [ISG_SOI ].[dbo].[AlertAnnotation] AS Notes
WHERE Notes.AlertID = AllAlerts.AlertID
FOR
XML PATH('') ), 1, 1, ''), '') AS Notes
FROM [ISG_SOI ].[dbo].[Alerts] AS AllAlerts
,[ISG_SOI ].[dbo].[AlertQueues] AS Queues
WHERE AllAlerts.AlertID IN ( SELECT QueueAssignment.AlertID
FROM [ISG_SOI ].[dbo].[AlertQueueAssignments] AS QueueAssignment
WHERE QueueAssignment.QueueID IN ( SELECT Queues.QueueID
WHERE Queues.QueueName = 'OCC' ) )
答案 0 :(得分:0)
您正在使用大量嵌套SELECTS,因为您可能正在评估每个上层行的SELECTS。而是使用公用表表达式(CTE),它使您能够提前完成SELECT。
WITH MyCTE AS (
SELECT distinct Services.Label
from [ISG_SOI ].[dbo].[SecureServiceCI] as Services
where Services.CIID in (
select Relationship.ServiceCIID
from [ISG_SOI ].[dbo].[CIRelationship] as Relationship
where Relationship.BNodeCIID = AllAlerts.CIID
)
SELECT
AllAlerts.AlertID as AlertID,
Queues.QueueID as QueueID,
Queues.QueueName as QueueName,
AllAlerts.ConnectorID as ConnectorID,
cast( ISNULL(STUFF ( (
select cast(',' as varchar(max)) + Services.Label
from (
MyCTE
) as Services
您可以为每个嵌套查询定义多个CTE
答案 1 :(得分:0)
根据我的评论说明部分解决方案:
from [ISG_SOI ].[dbo].[Alerts] AS AllAlerts
inner join [ISG_SOI ].[dbo].[QueueAssignment] as QA on QA.[AlertID] = AllAlerts.[AlertID]
inner join [ISG_SOI ].[dbo].[AlertQueues] AS Queues on Queues.[QueueID] = QA.[QueueID]
where Queues.QueueName = 'OCC'
而不是三个嵌套的子查询,只剩下一个。
可以对第一个for xml
子查询进行相同的改进 - 再次,你正在做一些使用子查询的连接。
答案 2 :(得分:0)
感谢大家的帮助!以下是基于评论输入的代码的最终结果!
SELECT
AllAlerts.AlertID as AlertID,
Queues.QueueID as QueueID,
Queues.QueueName as QueueName,
AllAlerts.ConnectorID as ConnectorID,
cast( ISNULL(STUFF ( (
select cast(',' as varchar(max)) + Services.Label
from (
SELECT distinct Services.Label
from [ISG_SOI ].[dbo].[SecureServiceCI] as Services
inner join [ISG_SOI ].[dbo].[CIRelationship] as Relationship
on Relationship.BNodeCIID = AllAlerts.CIID
where Services.CIID = Relationship.ServiceCIID
) as Services
for xml path ('')
), 1, 1, ''), '') as CHAR(1000)) as OwnedServices,
right(AllAlerts.DeviceID, len(AllAlerts.DeviceID)-charindex(',', AllAlerts.DeviceID)) as CIName,
AllAlerts.DeviceID as DeviceID,
AllAlerts.SituationMessage as Summary,
AllAlerts.AlertDetail as Detail,
AllAlerts.Acknowledged as Acknowledged,
AllAlerts.AssignedTo as AssignedTo,
AllAlerts.ReportedTime as CreatedTime,
AllAlerts.ClearedTime as ClearedTime,
AllAlerts.Severity as Severity,
AllAlerts.SvcDeskTicket as TicketID,
ISNULL(STUFF ( (
select cast('# ' as varchar(max)) + Notes.AnnotationText + '[' + Notes.CreatedBy + ', ' + cast(Notes.CreatedTime as varchar(max)) + ']'
from [ISG_SOI ].[dbo].[AlertAnnotation] as Notes
where Notes.AlertID = AllAlerts.AlertID
for xml path('')
), 1, 1, ''), '') as Notes
from
[ISG_SOI ].[dbo].[Alerts] as AllAlerts
inner join [ISG_SOI ].[dbo].[AlertQueueAssignments] as QA
on QA.[AlertID] = AllAlerts.[AlertID]
inner join [ISG_SOI ].[dbo].[AlertQueues] AS Queues
on Queues.[QueueID] = QA.[QueueID]
where Queues.QueueName = 'OCC'