一个棘手的SQL Intersect变体来自三个select语句

时间:2016-06-02 11:42:19

标签: sql sql-server

我有三个基于网站上三个过滤条件提供的参数构建的select语句。它们都有一个共同的PID列。有时这三个select语句返回记录,有时只有两个返回记录,一个将有0个记录,依此类推

当所有查询返回结果时,我只想要那些与所有三个相交的记录,如果其中两个返回结果那么结果应该只与返回的那两个相交,如果只有一个返回那么结果应该是查找记录的所有查询记录。当其中一个select语句返回0记录并且相同的问题与交叉时,联接失败。

这是实际代码

SELECT 
    pdf.ID, ldf.ID  
FROM   
    dbo.UserDefinedField (nolock) u               
INNER JOIN 
    dbo.LDFUserDefinedField (nolock) AS ldf ON u.ID = ldf.UserDefinedFieldID      
INNER JOIN 
    dbo.ParticipantUserDefinedField (nolock) AS pdf ON pdf.UserDefinedFieldID = u.ID         
                                                    AND (pdf.Value = ldf.Value OR ldf.Value = 'ALL') 
                                                    AND u.Name = 'CallType' AND ldf.ID = 69   

INTERSECT        

SELECT 
    pdf.ID, ldf.ID  
FROM   
    dbo.UserDefinedField (nolock) u               
INNER JOIN 
    dbo.LDFUserDefinedField (nolock) AS ldf ON u.ID = ldf.UserDefinedFieldID      
INNER JOIN 
    dbo.ParticipantUserDefinedField (nolock) AS pdf ON pdf.UserDefinedFieldID = u.ID         
                                                    AND (pdf.Value = ldf.Value OR ldf.Value = 'ALL') 
                                                    AND u.Name = 'CenterName' AND ldf.ID = 69 

INTERSECT   

SELECT 
    pdf.ID, ldf.ID   
FROM   
    dbo.UserDefinedField (nolock) u               
INNER JOIN 
    dbo.LDFUserDefinedField (nolock) AS ldf ON u.ID = ldf.UserDefinedFieldID      
INNER JOIN 
    dbo.ParticipantUserDefinedField (nolock) AS pdf ON pdf.UserDefinedFieldID = u.ID         
                                                    AND (pdf.Value = ldf.Value OR ldf.Value = 'ALL') 
                                                    AND u.Name = 'CenterID' AND ldf.ID = 69           

1 个答案:

答案 0 :(得分:0)

不确定这是否是最有效的解决方案,但根据您拥有的内容,我会使用内存表来提取完整​​的联合,然后消除您不想要的内容。返回结果的每个单独查询都将从其他两个查询中删除不匹配的结果。

Declare @tblPDF TABLE (
    pdfId int not null, 
    ldfId int not null, 
    SourceName varchar(25) not null
    )

Insert into @tblPDF (pdfId, ldfId, SourceName)
SELECT 
    pdf.ID, ldf.ID, u.Name 
FROM   
    dbo.UserDefinedField (nolock) u               
INNER JOIN 
    dbo.LDFUserDefinedField (nolock) AS ldf ON u.ID = ldf.UserDefinedFieldID      
INNER JOIN 
    dbo.ParticipantUserDefinedField (nolock) AS pdf ON pdf.UserDefinedFieldID = u.ID         
                                                    AND (pdf.Value = ldf.Value OR ldf.Value = 'ALL') 
                                                    AND u.Name = 'CallType' AND ldf.ID = 69   



Insert into @tblPDF (pdfId, ldfId, SourceName)
SELECT 
    pdf.ID, ldf.ID, u.Name 
FROM   
    dbo.UserDefinedField (nolock) u               
INNER JOIN 
    dbo.LDFUserDefinedField (nolock) AS ldf ON u.ID = ldf.UserDefinedFieldID      
INNER JOIN 
    dbo.ParticipantUserDefinedField (nolock) AS pdf ON pdf.UserDefinedFieldID = u.ID         
                                                    AND (pdf.Value = ldf.Value OR ldf.Value = 'ALL') 
                                                    AND u.Name = 'CenterName' AND ldf.ID = 69 

Insert into @tblPDF (pdfId, ldfId, SourceName)
SELECT 
    pdf.ID, ldf.ID, u.Name 
FROM   
    dbo.UserDefinedField (nolock) u               
INNER JOIN 
    dbo.LDFUserDefinedField (nolock) AS ldf ON u.ID = ldf.UserDefinedFieldID      
INNER JOIN 
    dbo.ParticipantUserDefinedField (nolock) AS pdf ON pdf.UserDefinedFieldID = u.ID        
                  AND (pdf.Value = ldf.Value OR ldf.Value = 'ALL') 
                                                    AND u.Name = 'CenterID' AND ldf.ID = 69         

IF exists(select 1 from @tblPDF where SourceName = 'Calltype')
  BEGIN
    Delete other
    From @tblPDF temp
    left outer join @tblPDF other on temp.pdfId = other.pdfId 
         and temp.ldfId = other.ldfid 
         and temp.SourceName <> other.SourceName
    Where temp.SourceName = 'CallType'
  END

IF exists(select 1 from @tblPDF where SourceName = 'CenterName')
  BEGIN
    Delete other
    From @tblPDF temp
    left outer join @tblPDF other on temp.pdfId = other.pdfId 
         and temp.ldfId = other.ldfid 
         and temp.SourceName <> other.SourceName
    Where temp.SourceName = 'CenterName'
  END

IF exists(select 1 from @tblPDF where SourceName = 'CenterID')
  BEGIN
    Delete other
    From @tblPDF temp
    left outer join @tblPDF other on temp.pdfId = other.pdfId 
         and temp.ldfId = other.ldfid 
         and temp.SourceName <> other.SourceName
    Where temp.SourceName = 'CenterID'
  END

select pdfId, ldfId from @tblPDF