我关注的数据包含培训,过滤器和用户记录。以下脚本将创建欲望表,需要根据某些规则应用过滤条件。
CREATE TABLE #Training (Id INT, Topic VARCHAR(50), [Status] VARCHAR(50));
INSERT INTO #Training SELECT 1, 'International Sales Training', 'Active';
INSERT INTO #Training SELECT 2, 'AMAR Sales Training', 'Active';
INSERT INTO #Training SELECT 3, 'APAC Sales Training', 'Active';
INSERT INTO #Training SELECT 4, 'General Training', 'Active';
INSERT INTO #Training SELECT 5, 'Manager Training', 'Active';
CREATE TABLE #Filter (Id INT, Condition VARCHAR(50), Parameter VARCHAR(50), TrainingId int);
INSERT INTO #Filter SELECT 1, 'Department', 'Sales', 1;
INSERT INTO #Filter SELECT 2, 'Division', 'International Sales', 2;
INSERT INTO #Filter SELECT 3, 'Place', 'AMAR', 2;
INSERT INTO #Filter SELECT 4, 'Designation', 'Manager', 1;
INSERT INTO #Filter SELECT 5, 'Designation', 'Developer', 4;
CREATE TABLE #User (Id INT, Name VARCHAR(50), Place VARCHAR(50), Country
VARCHAR(50), Department VARCHAR(50), Division VARCHAR(50), SubDivision
VARCHAR(50), [Location] VARCHAR(50), Designation VARCHAR(50));
INSERT INTO #User SELECT 1, 'John', 'EMEA', 'UK', 'Sales', 'International
Sales', '---', 'London', 'Manager';
INSERT INTO #User SELECT 2, 'Jim', 'AMAR', 'USA', 'Sales', 'International Sales', '---', 'New York', 'Manager';
INSERT INTO #User SELECT 3, 'Sally', 'AMAR', 'USA', 'Sales', 'International Sales', '---', 'Chicago', 'Sr. Manager';
INSERT INTO #User SELECT 4, 'Molly', 'AMAR', 'USA', 'Sales', 'International Sales', '---', 'New York', 'Manager';
INSERT INTO #User SELECT 5, 'Gorge', 'AMAR', 'USA', 'Sales', 'International Sales', 'FR relations', 'New York', 'Developer';
INSERT INTO #User SELECT 6, 'Kramer', 'APAC', 'Singapore', 'Sales', 'International Sales', '---', 'Singapore', 'Manager';
我正在努力获得匹配条件的过滤器培训记录,即下面的示例(对于UserId 2),它应该提供仅与Department和Designation匹配的培训详细信息。 TrainingID 1.但由于OR条件,我得到了3条记录。
Declare @Department VARCHAR(50),@Division VARCHAR(50), @Designation VARCHAR(50)
SELECT @Department = Department, @Division = Division,@Designation=Designation from #User where id=2
SELECT DISTINCT TR.ID
FROM #Training TR
LEFT OUTER JOIN #Filter F ON TR.ID = F.Id AND TR.Status = 'Active'
WHERE
(
(
(F.Condition='Department' and F.Parameter = @Department )
)
OR
(
(F.Condition='Division' and F.Parameter = @Division)
)
OR
(
(F.Condition='Designation' and F.Parameter = @Designation)
)
)
答案 0 :(得分:1)
如果您想匹配所有三个,请使用group by
和having
:
SELECT TR.ID
FROM #Training TR JOIN
#Filter F
ON TR.ID = F.Id AND TR.Status = 'Active'
WHERE (F.Condition = 'Department' and F.Parameter = @Department) OR
(F.Condition = 'Division' and F.Parameter = @Division) OR
(F.Condition = 'Designation' and F.Parameter = @Designation)
GROUP BY TR.ID
HAVING COUNT(DISTINCT F.Condition) = 3;
如果您不希望所有三个匹配,则可以更改HAVING
条款。
答案 1 :(得分:0)
请使用输出尝试此解决方案..
IWizardDescriptor[] wizards= PlatformUI.getWorkbench().getImportWizardRegistry().getPrimaryWizards();
<强>输出强>
Declare @Department VARCHAR(50),@Division VARCHAR(50), @Designation VARCHAR(50)
SELECT @Department = Department, @Division = Division,@Designation=Designation from #User where id=2
SELECT *
FROM #Training TR
LEFT OUTER JOIN #Filter F ON TR.ID = F.Id AND TR.Status = 'Active'
WHERE
F.Condition = 'Department' AND F.Parameter = @Department
OR F.Condition = 'Division' AND F.Parameter = @Division
OR F.Condition = 'Designation' AND F.Parameter = @Designation
答案 2 :(得分:0)
我认为用于加入表格的列有错误,需要在查询中进行一些修复,如下所示
SELECT DISTINCT TR.ID
FROM #Training TR
LEFT OUTER JOIN #Filter F ON TR.ID = F.TrainingId AND
TR.Status = 'Active' AND
((F.Condition='Department' and F.Parameter = @Department) OR
(F.Condition='Division' and F.Parameter = @Division) OR
(F.Condition='Designation' and F.Parameter = @Designation) )