在SQL

时间:2018-01-17 13:32:02

标签: sql-server join

我遵循包含各种过滤条件的过滤表。并且需要根据某些规则应用过滤条件。

Id  Condition   Parameter
1   Department  Sales
2   Division    International sales
3   Place       AMAR
4   Country     New York

过滤条件应该适用于以下样本记录,它应该返回所有匹配的记录,这些记录符合上述过滤条件,即所有记录,其中Department是'Sales',Division是'International sales',Place是'AMAR'和Country是'美国'。

Id  Name    Place   Country     Department  Division            Sub-Division        Location
1   John    EMEA    UK         Sales        International Sales ---         London
2   Jim     AMAR    USA        Sales        International Sales ---         New York
3   Sally   AMAR    USA        Sales        International Sales ---         Chicago
4   Molly   AMAR    USA         Sales       International Sales ---         New York
5   Gorge   AMAR    USA         Sales       International Sales FR relations    New York
6   Kramer  APAC    Singapore   Sales       International Sales ---         Singapore

但是以下查询不会返回预期的输出,

select *  from
Filter  T  
left outer join 
SampleData  T1 on (T.Parameter= 'Sales' AND T1.Department='Sales' ) 
AND (T.Parameter= 'International sales' AND T1.Division='International 
sales' 
)
AND (T.Parameter= 'New York' AND T1.Location='New York' ) 
AND (T.Parameter = 'AMAR' AND T1.Place='AMAR' )

2 个答案:

答案 0 :(得分:2)

我认为这可能更接近您的需求?

SELECT DISTINCT
    sd.* 
FROM 
    SampleData sd
    INNER JOIN [Filter] f1 ON f1.Condition = 'Department' AND sd.Department = f1.Parameter
    INNER JOIN [Filter] f2 ON f2.Condition = 'Division' AND sd.Division = f2.Parameter
    INNER JOIN [Filter] f3 ON f3.Condition = 'Place' AND sd.Place = f3.Parameter
    INNER JOIN [Filter] f4 ON f4.Condition = 'Country' AND sd.Country = f4.Parameter;

我还修复了你的数据/脚本的一些问题,并提出了这个工作示例:

CREATE TABLE #Filter (Id INT, Condition VARCHAR(50), Parameter VARCHAR(50));
INSERT INTO #Filter SELECT 1, 'Department', 'Sales';
INSERT INTO #Filter SELECT 2, 'Division', 'International Sales';
INSERT INTO #Filter SELECT 3, 'Place', 'AMAR';
INSERT INTO #Filter SELECT 4, 'Location', 'New York';
CREATE TABLE #SampleData (Id INT, Name VARCHAR(50), Place VARCHAR(50), Country VARCHAR(50), Department VARCHAR(50), Division VARCHAR(50), SubDivision VARCHAR(50), [Location] VARCHAR(50));
INSERT INTO #SampleData SELECT 1, 'John', 'EMEA', 'UK', 'Sales', 'International Sales', '---', 'London';
INSERT INTO #SampleData SELECT 2, 'Jim', 'AMAR', 'USA', 'Sales', 'International Sales', '---', 'New York';
INSERT INTO #SampleData SELECT 3, 'Sally', 'AMAR', 'USA', 'Sales', 'International Sales', '---', 'Chicago';
INSERT INTO #SampleData SELECT 4, 'Molly', 'AMAR', 'USA', 'Sales', 'International Sales', '---', 'New York';
INSERT INTO #SampleData SELECT 5, 'Gorge', 'AMAR', 'USA', 'Sales', 'International Sales', 'FR relations', 'New York';
INSERT INTO #SampleData SELECT 6, 'Kramer', 'APAC', 'Singapore', 'Sales', 'International Sales', '---', 'Singapore';
SELECT
    sd.* 
FROM 
    #SampleData sd
WHERE
    sd.Department = (SELECT Parameter FROM #Filter WHERE Condition = 'Department')
    AND sd.Division = (SELECT Parameter FROM #Filter WHERE Condition = 'Division')
    AND sd.Place = (SELECT Parameter FROM #Filter WHERE Condition = 'Place')
    AND sd.Location = (SELECT Parameter FROM #Filter WHERE Condition = 'Location');

给出这些结果,即匹配过滤器的人:

Id  Name    Place   Country Department  Division                SubDivision     Location
2   Jim     AMAR    USA     Sales       International Sales     ---             New York
4   Molly   AMAR    USA     Sales       International Sales     ---             New York
5   Gorge   AMAR    USA     Sales       International Sales     FR relations    New York

答案 1 :(得分:0)

我认为你的第四条规则是位置,而不是国家,比如

4   Location     New York

以下代码将动态运行,您将多少规则添加到规则表中:

DECLARE @tmp VARCHAR(250)
DECLARE @Query VARCHAR(max)
SET @tmp = ''
SELECT @tmp = @tmp + Condition +  ' = ''' + Parameter + ''' and ' from FilterTable

SELECT @tmp = SUBSTRING(@tmp,0,LEN(@tmp)-3)
SELECT @tmp
SELECT @Query = 'Select * from SampleData where '+ @tmp

EXEC (@Query)

希望这会有所帮助,即使它的成本也很高。

Tested