如何将使用 STUFF 的查询限制为每组填充值仅 return 一条记录,而不是每个填充项一条记录?
How can I restrict a query that uses STUFF to only return one record for each group of values stuffed rather than one for each stuffed item?
使用此查询:
SELECT S.Unit, LU.ReportName, S.NextExecution,
STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses,
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
...我想获得与 RreportsScheduler table 中存在的记录相同的记录数,但实际上我为每个相关的电子邮件地址(EmailAddr FROM ReportsUnitEmails)获得了一条记录。
一个邮箱地址,一个记录return;有四个电子邮件地址的地方,有四个记录 returned;等等
所以问题是:我可以 "distinctify" 复杂查询只对每个电子邮件地址 return 一条记录(同时仍然将它们全部塞入 "AllEmailAddresses")。
我试过这个:
SELECT S.Unit, LU.ReportName, S.NextExecution,
DISTINCT(STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses),
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
...但它拒绝编译 [ile,ly],说,“错误 156:关键字 'DISTINCT' 附近的语法不正确。
关键字 'FOR'."
附近的语法不正确
不是 SQLhead,无论如何它只是在黑暗中拍摄。
我怎样才能拥有我所有的东西,但更蓬松,而不是更闷?我想知道自连接是否会在我的未来出现,但我不知道是否真的如此,如果是,如何实现它。
更新
接受的答案和这个:
SELECT DISTINCT S.Unit, LU.ReportName, S.NextExecution, S.ReportID,
STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses,
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
...工作,但我会接受公认的答案,因为看起来 "more better."
您只需要 ReportsUnitEmails
table 在您 STUFF
的子查询中。我在外部查询中注释掉了 ReportsUnitEmails
的连接。那应该可以。
SELECT S.Unit, LU.ReportName, S.NextExecution,
STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses,
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
--FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
您的 DISTINCT 后应跟有 SELECT 语句。 SELECT 之前不能有 DISTINCT。尝试将 DISTINCT 与 SELECT 语句一起使用。
使用此查询:
SELECT S.Unit, LU.ReportName, S.NextExecution,
STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses,
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
...我想获得与 RreportsScheduler table 中存在的记录相同的记录数,但实际上我为每个相关的电子邮件地址(EmailAddr FROM ReportsUnitEmails)获得了一条记录。
一个邮箱地址,一个记录return;有四个电子邮件地址的地方,有四个记录 returned;等等
所以问题是:我可以 "distinctify" 复杂查询只对每个电子邮件地址 return 一条记录(同时仍然将它们全部塞入 "AllEmailAddresses")。
我试过这个:
SELECT S.Unit, LU.ReportName, S.NextExecution,
DISTINCT(STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses),
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
...但它拒绝编译 [ile,ly],说,“错误 156:关键字 'DISTINCT' 附近的语法不正确。 关键字 'FOR'."
附近的语法不正确不是 SQLhead,无论如何它只是在黑暗中拍摄。
我怎样才能拥有我所有的东西,但更蓬松,而不是更闷?我想知道自连接是否会在我的未来出现,但我不知道是否真的如此,如果是,如何实现它。
更新
接受的答案和这个:
SELECT DISTINCT S.Unit, LU.ReportName, S.NextExecution, S.ReportID,
STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses,
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
...工作,但我会接受公认的答案,因为看起来 "more better."
您只需要 ReportsUnitEmails
table 在您 STUFF
的子查询中。我在外部查询中注释掉了 ReportsUnitEmails
的连接。那应该可以。
SELECT S.Unit, LU.ReportName, S.NextExecution,
STUFF((SELECT ','+EmailAddr FROM ReportsUnitEmails E WHERE S.Unit = E.Unit AND S.ReportID = E.ReportID FOR XML PATH('')),1,1,'') AS AllEmailAddresses,
S.NextExecutionsBeginDateArg, S.NextExecutionsEndDateArg
FROM ReportsScheduler S
FULL JOIN ReportsLU LU ON S.ReportID = LU.ReportID
--FULL JOIN ReportsUnitEmails E on S.Unit = E.Unit AND S.ReportID = E.ReportID
ORDER BY S.Unit, S.ReportID
您的 DISTINCT 后应跟有 SELECT 语句。 SELECT 之前不能有 DISTINCT。尝试将 DISTINCT 与 SELECT 语句一起使用。