Sql intersect conditional
Asked Answered
S

3

6

I want to know if we can do an intersect conditional. theres is somes query, but the result is wrong (always empty). I write what it should result.

DECLARE @CAN_USE_TABLE1 BIT
DECLARE @CAN_USE_TABLE2 BIT
DECLARE @CAN_USE_TABLE3 BIT
DECLARE @CAN_USE_TABLE4 BIT

DECLARE @TABLE1 AS TABLE ( ABC INT ) -- values will be 1,2,3
DECLARE @TABLE2 AS TABLE ( ABC INT ) -- values will be 1,2
DECLARE @TABLE3 AS TABLE ( ABC INT ) --EMPTY TABLE
DECLARE @TABLE4 AS TABLE ( ABC INT ) --EMPTY TABLE
INSERT INTO @TABLE1 VALUES (1)
INSERT INTO @TABLE1 VALUES (2)
INSERT INTO @TABLE1 VALUES (3)
INSERT INTO @TABLE2 VALUES (1)
INSERT INTO @TABLE2 VALUES (2)

SET @CAN_USE_TABLE1 = 1
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 1
SET @CAN_USE_TABLE4 = 0

SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1 = 1
INTERSECT
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2 = 1
INTERSECT 
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3 = 1
INTERSECT
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4 = 1

--RESULT SHOULD BE :
--  NO RESULT
--
--  BECAUSE, AT THIS STAGE, TABLE1 AND TABLE2 AND TABLE3 SHOULD BE INTERSECTED. AND BECAUSE TABLE3 IS EMPTY, THE RESULT IS EMPTY.

SET @CAN_USE_TABLE1 = 1
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 0
SET @CAN_USE_TABLE4 = 0

SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1 = 1
INTERSECT
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2 = 1
INTERSECT 
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3 = 1
INTERSECT
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4 = 1

--RESULT SHOULD BE :
--  1
--  2
--
--  BECAUSE, AT THIS STAGE, TABLE1 AND TABLE2 SHOULD BE INTERSECTED

SET @CAN_USE_TABLE1 = 0
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 0
SET @CAN_USE_TABLE4 = 0

SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1 = 1
INTERSECT
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2 = 1
INTERSECT 
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3 = 1
INTERSECT
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4 = 1

--RESULT SHOULD BE :
--  1
--  2
--
--  BECAUSE, AT THIS STAGE, ONLY TABLE 2 SHOULD BE USED
Stormproof answered 25/5, 2012 at 19:36 Comment(1)
Are you looking for omitting the intersect of tables whene they associated variables are set to 0?Bondstone
C
2

An empty set INTERSECTed with any other set will always be empty. It's like multiplying by 0. You always get 0.

Conditional INTERSECTing will require either a dynamic query, or a staging table, like this:

Initializations

DECLARE @CAN_USE_TABLE1 BIT
DECLARE @CAN_USE_TABLE2 BIT
DECLARE @CAN_USE_TABLE3 BIT
DECLARE @CAN_USE_TABLE4 BIT

DECLARE @TABLE1 AS TABLE ( ABC INT )
DECLARE @TABLE2 AS TABLE ( ABC INT )
DECLARE @TABLE3 AS TABLE ( ABC INT )
DECLARE @TABLE4 AS TABLE ( ABC INT )
DECLARE @RESULT AS TABLE ( ABC INT ) --Adding this result table
INSERT INTO @TABLE1 VALUES (1)
INSERT INTO @TABLE1 VALUES (2)
INSERT INTO @TABLE1 VALUES (3)
INSERT INTO @TABLE2 VALUES (1)
INSERT INTO @TABLE2 VALUES (2)

SET @CAN_USE_TABLE1 = 1
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 0
SET @CAN_USE_TABLE4 = 0

Processing

INSERT INTO @RESULT
SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1=1 UNION
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2=1 UNION
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3=1 UNION
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4=1

DELETE r FROM @RESULT r
WHERE NOT EXISTS(SELECT 1 FROM @TABLE1 WHERE ABC=r.ABC)
AND @CAN_USE_TABLE1=1;

DELETE r FROM @RESULT r
WHERE NOT EXISTS(SELECT 1 FROM @TABLE2 WHERE ABC=r.ABC)
AND @CAN_USE_TABLE2=1;

DELETE r FROM @RESULT r
WHERE NOT EXISTS(SELECT 1 FROM @TABLE3 WHERE ABC=r.ABC)
AND @CAN_USE_TABLE3=1;

DELETE r FROM @RESULT r
WHERE NOT EXISTS(SELECT 1 FROM @TABLE4 WHERE ABC=r.ABC)
AND @CAN_USE_TABLE4=1;

SELECT * FROM @RESULT;

Result

1
2
Cristal answered 25/5, 2012 at 20:34 Comment(2)
The delete statement will not remove elements from '@Result' if '@CAN_USE_TABLE1' = 0. you did an unconditional union but a conditional deleteBondstone
@anouar204 Good catch. Resolved by adding WHERE @CAN_USE_TABLE... to the UNION SELECTsCristal
B
0
declare @sql nvarchar(4000),
    @params nvarchar(4000)

 if @can_use_table1 = 1
    select @sql= 'select abc from @table1'

 if @can_use_table2 = 1
 begin
    if  @can_use_table1 = 1
       select @sql = @sql + ' intersect '
    select @sql = @sql + 'select abc from @table2'
 end

 if @can_use_table3 = 1
 begin
   if  @can_use_table1 = 1 or @can_use_table2 = 1
      select @sql= @sql + ' intersect '        
   select @sql= @sql + 'select abc from @table3'
 end

 if @can_use_table4 = 1
 begin
   if  @can_use_table1 = 1 or @can_use_table2 = 1 or @can_use_table3 = 1
      select @sql= @sql + ' intersect ' 
   select @sql= @sql + 'select abc from @table4'    
 end

select @params ='@can_use_table1 bit, @can_use_table2 bit,
         @can_use_table3 bit, @can_use_table4 bit,
 @table1 table, @table2 table, @table3  table, @table4 table' 

exec sp_executesql @sql,@params, 
       @can_use_table1,@can_use_table2, @can_use_table3, @can_use_table4,
       @table1, @table2, @table3, @table4 
Bondstone answered 25/5, 2012 at 20:19 Comment(0)
P
0

You could do this in a single query as:

select ABC
from (SELECT ABC, @CAN_USE_TABLE1 as CanUse1, 0 as CanUse2, 0 as CanUse3, 0 as CanUse4
      FROM @TABLE1 
      union all
      SELECT ABC, 0, @CAN_USE_TABLE2, 0, 0 FROM @TABLE2
      union all
      SELECT ABC, 0, 0,  @CAN_USE_TABLE3, 0 FROM @TABLE3
      union all
      SELECT ABC, 0, 0, 0, @CAN_USE_TABLE4 FROM @TABLE4
     ) t
group by ABC
having max(Canuse1) = @CAN_USE_TABLE1 AND
       max(CanUse2) = @CAN_USE_TABLE2 AND
       max(CanUse3) = @CAN_USE_TABLE3 And
       max(CanUse4) = @CAN_USE_TABLE4
Prosthetics answered 26/5, 2012 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.