简体   繁体   中英

How can I select from one table where the ID exists in one of two tables, but not in either of other two tables

This sounds confusing, but the idea is quite simple.

I want to get a list of products that have default rates, but a given 'Agent' doesn't have rates for. To do that, I need to select from the below table

    t_Products
|-ProductID-|--Product-|
|   100     | Product1 |
|   101     | Product2 |
|   102     | product3 |
|   103     | product4 |

Where the ID exists in either t_Annual_DefaultCost or t_Daily_DefaultCost

    t_Annual_DefaultCost
|-DefaultID-|-ProductID-|--Cost-|
|    100    |     100   | 24.00 |
|    101    |     101   | 26.00 |

    t_Daily_DefaultCost
|-DefaultID-|-ProductID-|--Cost-|-Days-|
|    100    |     100   | 24.00 |   1  |
|    101    |     100   | 26.00 |   2  |
|    102    |     102   | 22.50 |   2  |
|    103    |     102   | 97.50 |   8  |

But it cannot exist in either t_Annual_AgentCost or t_Daily_AgentCost for the given Agent ID

    t_Annual_AgentCost 
|---CostID--|-ProductID-|-AgentID-|--Cost-|
|    100    |    100    |  10001  | 24.00 |
|    101    |    100    |  10001  | 20.00 |

    t_Daily_AgentCost
|---CostID--|-ProductID-|-AgentID-|--Cost-|-Days-|
|    100    |     100   |  10001  | 24.00 |   1  |
|    102    |     102   |  10002  | 35.00 |   2  |

so for AgentID 10001 the end result should be

|-ProductID-|--Product-|
|   101     | product2 |
|   102     | product3 |

and for AgentID 10002 the end result should be

|-ProductID-|--Product-|
|   100     | product1 |
|   101     | product2 |

I'm currently using the below code to get a list of products that have default rates. But I can't work out how to remove/not get the ones also in the AgentCost tables.

Select 
    distinct a.* 
from 
    t_Products as a
inner join
    ( 
        select 
            DefaultID ,ProductID
        from 
            t_Daily_DefalutCost 

        union 

        select 
            DefaultID , ProductID
        from 
            t_Annual_DefaultCost 
    ) 
    as b on a.ProductID = b.ProductID

If you want to do one Agent at a time, then this is how I would do it:

SELECT
    a.* 
FROM
    t_Products As a
WHERE
    (   EXISTS( SELECT * FROM t_Daily_DefaultCost  As d WHERE d.ProductID = a.ProductID )
     OR EXISTS( SELECT * FROM t_Annual_DefaultCost As d WHERE d.ProductID = a.ProductID )
     )
AND NOT
    (   EXISTS( SELECT * FROM t_Daily_AgentCost    As d 
                WHERE d.ProductID = a.ProductID
                AND   d.AgentID   = @SpecifedAgentID )
     OR EXISTS( SELECT * FROM t_Annual_AgentCost   As d 
                WHERE d.ProductID = a.ProductID
                AND   d.AgentID   = @SpecifedAgentID )
     )

The OR EXISTS s here work pretty much the same as UNION ALL SELECT s would .

I'd use this sort of approach.

select yourfields
from yourtables
where id in 
(

(select id
 from onetable
 union
 select id
 from anothertable)
 except
 (select id
  from yetanothertable
  union
  select id
  from thefinaltable)
)

You can fill in the actual table names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM