我有一个存储过程,用于获取公告板的通信数据。默认情况下,它会查找位于查看器部门中的所有内容。但是我允许他们选择多个部门来查看公司周围的内容,如果他们选择..
我面临的问题是,对于他们选择的每个部门,它会多次提供相同的记录。
在我的查询中,在WHERE
子句中,我有一个说AND aud.Department IN (SELECT department FROM @departments)
的声明。
这是发生错误的地方。对于该临时表中的每个部门,它返回相同的记录。
如何从他的桌子中仅选择不同的记录? commID
是身份
-- Fetch our communicatons based on certain criteria
SELECT sub.commID,
sub.status,
sub.content,
CONVERT (VARCHAR (100), sub.distributionDate, 101) AS distributionDate,
sub.subject,
a.FirstName + ' ' + a.LastName AS approver,
b.FirstName + ' ' + b.LastName AS poc,
b.NTID AS NTID,
CONVERT (VARCHAR (100), sub.dateApproved, 101) AS dateApproved,
(SELECT department
FROM comm_audience
WHERE commID = sub.commID
FOR XML PATH ('departments'), ELEMENTS, TYPE, ROOT ('audience'))
FROM comm_submissions AS sub
INNER JOIN
comm_medium AS med
ON sub.commID = med.commID
INNER JOIN
comm_mediumOptions AS mop
ON med.mediumID = mop.mediumID
INNER JOIN
comm_audience AS aud
ON sub.CommID = aud.commID
LEFT OUTER JOIN
dbo.EmployeeTable AS a
ON sub.approver = a.QID
LEFT OUTER JOIN
dbo.EmployeeTable AS b
ON sub.poc = b.QID
WHERE mop.mediumTitle IN ('Digital eBoard') -- Only eBoard Comms
AND aud.Department IN (SELECT department FROM @departments) -- Viewers Department must be in the audience
AND sub.commLocation = @locale -- The comm locale must be the viewers locale
AND sub.status IN ('Completed') -- Only show Approved or Completed Comms
ORDER BY sub.distributionDate DESC
- 更新
-- Create a temp table of our department(s) we are viewing
DECLARE @departments as TABLE (department varchar(100))
INSERT INTO @departments(department)
SELECT ParamValues.x1.value('department[1]', 'varchar(100)')
FROM @xml.nodes('/root/data/departments/audience') AS ParamValues(x1);
答案 0 :(得分:2)
所以我的理解是您的表 comm_audience 包含您需要过滤的部门,但每个commID可以有多个行。所以在where子句中尝试过滤掉那些不在你的部门列表中的commID:
SELECT sub.commID,
sub.status,
sub.content,
CONVERT (VARCHAR (100), sub.distributionDate, 101) AS distributionDate,
sub.subject,
a.FirstName + ' ' + a.LastName AS approver,
b.FirstName + ' ' + b.LastName AS poc,
b.NTID AS NTID,
CONVERT (VARCHAR (100), sub.dateApproved, 101) AS dateApproved,
(SELECT department
FROM comm_audience
WHERE commID = sub.commID
FOR XML PATH ('departments'), ELEMENTS, TYPE, ROOT ('audience'))
FROM comm_submissions AS sub
INNER JOIN
comm_medium AS med
ON sub.commID = med.commID
INNER JOIN
comm_mediumOptions AS mop
ON med.mediumID = mop.mediumID
--Don't join here
--INNER JOIN
--(SELECT DISTINCT commID FROM comm_audience) AS aud
--ON sub.CommID = aud.commID
LEFT OUTER JOIN
dbo.EmployeeTable AS a
ON sub.approver = a.QID
LEFT OUTER JOIN
dbo.EmployeeTable AS b
ON sub.poc = b.QID
WHERE mop.mediumTitle IN ('Digital eBoard') -- Only eBoard Comms
--Comment this out
--AND aud.Department IN (SELECT department FROM @departments) -- Viewers Department must be in the audience
--What you want is to filter out an commID that aren't in your list of departments so try this
AND sub.CommID IN (SELECT DISTINCT commID FROM comm_audience WHERE Department IN (SELECT department FROM @departments))
AND sub.commLocation = @locale -- The comm locale must be the viewers locale
AND sub.status IN ('Completed') -- Only show Approved or Completed Comms
ORDER BY sub.distributionDate DESC