表C结构如下
   Company nvarchar(2)  --公司 主健
   Agencyid nvarchar(10) --工号 主健
   Policy nvarchar(8) --保单号  主健
   Crtable nvarchar(4) –险种代码 主健
   Life nvarhar(2)
   Coverage nvarchar(2)
   Rider nvarchar(2)
   要求编写SQL语句查询(companycode,policycode,crtable)组合键值重复的记录明细。

解决方案 »

  1.   

    select *
    from tc c
    where exists(select 1 from tc where companycode = c.companycode and policycode = c.policycode and crtable = c.crtable and agencyid != c.agencyid)
      

  2.   

    select t.* from c t where exists 
    (
      select 1 from (select Company , Policy , Crtable from c group by Company , Policy , Crtable having count(1) > 1) m 
      where Company = t.Company and Policy and t.Policy and Crtable and t.Crtable
    )
    order by t.Company , t.Policy , t.Crtable
      

  3.   

    select
     * 
    from
     c t 
    where
     exists(select 1 from c where companycode = t.companycode and policycode = t.policycode and crtable = t.crtable and agencyid <> t.agencyid)
      

  4.   

    select * from c as a
    where (select count(*) from c where company=a.company and policy=a.policy and crtable=a.crtable)>1
    order by company,policy,crtable