有  表1:
   幢ID   幢属性 
   001     AAA 
   002     BBB表 2:
  幢ID   幢类型
  001     aaa 
  002     aaa
  002     bbb要求字段: 幢ID, 幢属性, 幢类型;条件: 幢类型为  aaa, 且仅仅为 aaa的 记录集(比如说,幢ID为002时,它的幢类型为 aaa and bbb两种,这种不能选取,只选取 幢ID 为001,它的幢类型 仅仅只有 aaa类型的这类数据)。多谢各位~~

解决方案 »

  1.   


    with b as(
      select 幢ID,幢类型
        from (select 幢ID,幢类型,count(幢ID)over(partition by 幢ID) cnt from t2) 
       where cnt = 1 and 幢类型 = 'aaa'
    )
     select a.幢ID,a.幢属性,b.幢类型
       from 表1 a inner join b on a.幢ID=b.幢ID;
      

  2.   


    --t2 指的是表2
    with b as(
      select 幢ID,幢类型
        from (select 幢ID,幢类型,count(幢ID)over(partition by 幢ID) cnt from 表2) 
       where cnt = 1 and 幢类型 = 'aaa'
    )
     select a.幢ID,a.幢属性,b.幢类型
       from 表1 a inner join b on a.幢ID=b.幢ID;
      

  3.   

     select t1.id,t1.lx,count(t1.id) cnt from
       t1,t2
       having t1.id = t2.id and t1.lx = t2.lx  and count(t1.id) = 1
       group by t1.id,t1.lx ,t2.id,t2.lx   试试可行