Oracle对许多相同的表

时间:2019-02-25 04:33:05

标签: sql sql-server database oracle relational-database

我必须提取具有跨越多个合同ID的位置ID的记录,还应排除重复的记录。下面是一个示例表。第二张表是具有我想要的结果的查询。

| CONTRACT ID | LOCATION ID | CONTRACT NAME | CONTRACT DATE | CONTRACT STATUS |
--------------------------------------------------------------------------
|  CT1          |  100      | MICROSOFT     | 12/09/2029    |   PENDING        
|  CT1          |  100      | MICROSOFT     | 12/09/2029    |   APPROVED       
|  CT3          |  155      | YAHOO         | 02/03/2030    |   EXPIRED       
|  CT4          |  180      | ADOBE         | 02/03/2030    |   IN LITIGATION       
|  CT4          |  180      | ADOBE         | 02/03/2030    |   APPROVED       
|  CT5          |  199      | YAHOO         | 02/03/2030    |   PENDING       
|  CT6          |  100      | GOOGLE        | 10/23/2028    |   PENDING       
|  CT7          |  155      | UBER          | 05/15/2027    |   PENDING       
---------------------------------------------------------------------------

| CONTRACT ID   | LOCATION ID  |
----------------------------------
|  CT1          |  100         |
|  CT6          |  100         |
|  CT3          |  155         |
|  CT7          |  155         |
-----------------------------------

我尝试运行此查询,但它同时包含两个CT4 Adob​​e合同的记录,这些合同的位置ID不能跨越多个合同ID。 即使我在查询的开头放置了一个与众不同的内容,该合同CB4 ID /位置ID对也不应成为结果的一部分。

SELECT contract_id, location_id from random_table where location_id in
    (SELECT location_id FROM random_table
    where (location_id is not null)
    group by location_id  having count( location_id) > 1 )
    group by contract_id, location_id
    order by location_id

2 个答案:

答案 0 :(得分:1)

您可以尝试使用var start = $('.content').first(); var lastScrollTop = 0; $(document).scroll(function(event){ var st = $(this).scrollTop(); if (st > lastScrollTop){ if($(this).scrollTop()>= start.position().top){ updateClass(); start = start.next(); } } else { if($(this).scrollTop()<= start.position().top){ updateClass(); start = start.prev(); } } lastScrollTop = st; }); function updateClass(){ $(".content").removeClass("active"); start.addClass("active"); } 进行以下操作。

WHERE EXISTS

Online Demo

编辑:

如果要查找此类位置ID,可以使用以下查询。

SELECT contractid, 
       locationid 
FROM   [youtable] y 
WHERE  EXISTS (SELECT 1 
               FROM   [youtable] TY 
               WHERE  TY.locationid = Y.locationid 
                      AND TY.contractid <> Y.contractid) 
GROUP  BY contractid, 
          locationid 
ORDER  BY locationid 

答案 1 :(得分:0)

您可以在下面尝试-使用相关子查询

DEMO

select distinct location_id, contractid
from tablename a
where exists (select 1 from tablename b where a.location_id=b.location_id
having count(distinct b.contractid)>1)

输出:

LOCATIONID  contractid
100           CT1
155           CT3
100           CT6
155           CT7