现有职工表(姓名,科室名称,组内排序)
name  ksmc  znpx
A1    语文   1
A2    语文   2
A3    语文   3
A4    数学   1
A5    数学   2
A6    数学   2A6 的组内排序与A5的重复了,我想检测每个科室中重复的znpx的记录

解决方案 »

  1.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([name] varchar(2),[ksmc] varchar(4),[znpx] int)
    insert [tb]
    select 'A1','语文',1 union all
    select 'A2','语文',2 union all
    select 'A3','语文',3 union all
    select 'A4','数学',1 union all
    select 'A5','数学',2 union all
    select 'A6','数学',2select * from [tb] t
    where znpx in (select znpx from [tb]
    where ksmc=t.ksmc group by znpx having count(1) >= 2)
    ---------------------
    A5 数学 2
    A6 数学 2
      

  2.   


    --> 测试数据: 职工表
    if object_id('职工表') is not null drop table 职工表
    create table 职工表 (name varchar(2),ksmc varchar(4),znpx int)
    insert into 职工表
    select 'A1','语文',1 union all
    select 'A2','语文',2 union all
    select 'A3','语文',3 union all
    select 'A4','数学',1 union all
    select 'A5','数学',2 union all
    select 'A6','数学',2select * from 职工表 a
    where exists(select 1 from 职工表 where ksmc=a.ksmc and znpx=a.znpx and name!=a.name)
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2010-01-20 09:16:45
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([name] varchar(2),[ksmc] varchar(4),[znpx] int)
    insert [tb]
    select 'A1','语文',1 union all
    select 'A2','语文',2 union all
    select 'A3','语文',3 union all
    select 'A4','数学',1 union all
    select 'A5','数学',2 union all
    select 'A6','数学',2
    --------------开始查询--------------------------
    select * from [tb] t where exists(select 1 from tb where name<>t.name and ksmc=t.ksmc and znpx=t.znpx)
    ----------------结果----------------------------
    /* name ksmc znpx
    ---- ---- -----------
    A5   数学   2
    A6   数学   2(2 行受影响)
    */
      

  4.   

        create table #a
    (
    name varchar(10),
    ksmc varchar(10),
    znpx int
    )
    insert into #a
    select 'A1',    '语文',  1 union all 
    select 'A2',    '语文',  2 union all
    select 'A3',    '语文',  3 union all
    select 'A4',    '数学',  1 union all
    select 'A5',    '数学',  2 union all
    select 'A6',    '数学',  2 --select * from #a
    select * from #a a where exists(select 1 from #a where ksmc=a.ksmc and znpx=a.znpx and name<a.name)