我需要编写一个查询,该查询将显示按组接收电子邮件的收件人列表,在本例中为AddressType
:
从下面的屏幕快照中可以看到,我能够正确PIVOT
AddressType
NotificationRecipientType
并使用STUFF
值,在某些情况下,问题是我有2 CC或者它们可以更多取决于数据。
在查询结束时,它仅显示第一个值而不是多个值,我想将它们添加为逗号分隔,这是我考虑使用/*
Base Hierarchy query to display all data
*/
SELECT
ns.ScheduleName AS NotificationName,
ns.[Description] AS NotificationDescription,
ne.EventName,
nt.[Type] AS NotificationType,
nrt.[Description] AS NotificationRecipientType,
nat.AddressType
FROM
dbo.[Notification] n
JOIN
dbo.NotificationSchedule ns ON ns.NotificationScheduleID = n.NotificationScheduleID AND ns.Active = 1
JOIN
dbo.NotificationEvent ne ON ne.NotificationEventID = ns.NotificationEventID
JOIN
dbo.NotificationType nt ON nt.NotificationTypeID = n.NotificationTypeID
JOIN
dbo.NotificationRecipient nr ON nr.NotificationID = n.NotificationID AND nr.Active = 1
JOIN
dbo.NotificationRecipientType nrt ON nrt.RecipientTypeID = nr.RecipientTypeID
JOIN
dbo.NotificationAddressType nat ON nat.AddressTypeID = nr.AddressTypeID
WHERE
n.Active = 1
ORDER BY
n.NotificationID
/*
Dynamic SQL query to retrieve all possible AddressType values by NotificationRecipientType
*/
DECLARE
@cols AS NVARCHAR(MAX) = NULL
,@query AS NVARCHAR(MAX) = NULL
SELECT @cols =
STUFF
(
(
SELECT
',' + QUOTENAME(AddressType)
FROM
dbo.NotificationAddressType
FOR XML PATH('')
), 1, 1, ''
)
SET @query = N'
SELECT * FROM
(
SELECT
ns.ScheduleName AS NotificationName,
ns.[Description] AS NotificationDescription,
nrt.[Description] AS NotificationRecipientType,
nat.AddressType
--,ROW_NUMBER() OVER(PARTITION BY n.NotificationID ORDER BY nat.AddressTypeID) AS RN
FROM
dbo.[Notification] n
JOIN
dbo.NotificationSchedule ns ON ns.NotificationScheduleID = n.NotificationScheduleID AND ns.Active = 1
JOIN
dbo.NotificationEvent ne ON ne.NotificationEventID = ns.NotificationEventID
JOIN
dbo.NotificationType nt ON nt.NotificationTypeID = n.NotificationTypeID
JOIN
dbo.NotificationRecipient nr ON nr.NotificationID = n.NotificationID AND nr.Active = 1
JOIN
dbo.NotificationRecipientType nrt ON nrt.RecipientTypeID = nr.RecipientTypeID
JOIN
dbo.NotificationAddressType nat ON nat.AddressTypeID = nr.AddressTypeID
WHERE
n.Active = 1
) t
PIVOT
(
MIN([NotificationRecipientType])
FOR AddressType IN (' + @cols + ')
) p
'
EXEC sp_executesql @query
方法的地方,但是现在,因为它是动态SQL查询,所以在PIVOT部分中停留在如何使用它上。
这是我到目前为止的查询:
"policy": {
"validation": {
"minimumSizeMB": 0.000005
},
"externalData": {
"retry": 3,
"retryInterval":60,
"longRetry": 2,
"longRetryInterval": "01:00:00"
}
}