用户编号 姓名      角色            模块代码   模块名称
0000 A 系统管理员 01 生产管理
0000 A 系统管理员 02 库存管理
0000 A 系统管理员 03 采购管理
0000 A 系统管理员 04 销售管理
0000 A 系统管理员 05 系统管理
0000 A 操作员     05 系统管理如何在用select查询的时候把最后两条重复的,去掉一条,  最后两条角色虽然不同,但 模块相同

解决方案 »

  1.   

    select * from tablename  a
    where not exists(select 1  from tablename where 模块代码 = a.模块代码  and 模块名称 = a.模块名称 and 角色 > a.角色)
      

  2.   

    select m.* from tb m,
    (select 模块名称,max(角色) from tb group by 模块名称) n
    where m.模块名称=n.模块名称 and m.角色 = n.角色max那个地方用min也行.
      

  3.   

    select * from tablename  a
    where not exists(select 1  from tablename where 模块代码 = a.模块代码 and 角色 > a.角色)
      

  4.   

    create table T(用户编号 char(4), 姓名 char(1),      角色 nvarchar(10),           模块代码 char(2),   模块名称 nvarchar(10))
    insert T select '0000', 'A', '系统管理员', '01', '生产管理'
    union all select '0000', 'A', '系统管理员', '02', '库存管理'
    union all select '0000', 'A', '系统管理员', '03', '采购管理'
    union all select '0000', 'A', '系统管理员', '04', '销售管理'
    union all select '0000', 'A', '系统管理员', '05', '系统管理'
    union all select '0000', 'A', '操作员',     '05', '系统管理'select max(用户编号) as 用户编号,
    max(姓名) as 姓名,
    max(角色) as 角色,
    max(模块代码) as 模块代码,
    模块名称
    from T group by 模块名称
    order by 模块代码--result
    用户编号 姓名   角色         模块代码 模块名称       
    ---- ---- ---------- ---- ---------- 
    0000 A    系统管理员      01   生产管理
    0000 A    系统管理员      02   库存管理
    0000 A    系统管理员      03   采购管理
    0000 A    系统管理员      04   销售管理
    0000 A    系统管理员      05   系统管理(5 row(s) affected)
      

  5.   

    declare @ta table (用户编号 char(4), 姓名 char(1),      角色 nvarchar(10),           模块代码 char(2),   模块名称 nvarchar(10))
    insert @ta select '0000', 'A', '系统管理员', '01', '生产管理'
    union all select '0000', 'A', '系统管理员', '02', '库存管理'
    union all select '0000', 'A', '系统管理员', '03', '采购管理'
    union all select '0000', 'A', '系统管理员', '04', '销售管理'
    union all select '0000', 'A', '系统管理员', '05', '系统管理'
    union all select '0000', 'A', '操作员',     '05', '系统管理'select * from @ta a where not exists (select 1 from @ta where  模块名称=a.模块名称 and 模块代码=a.模块代码
    and  角色<a.角色)select * from @ta a where not exists (select 1 from @ta where  模块名称=a.模块名称 and 模块代码=a.模块代码
    and  角色>a.角色)-->决定保留的记录
      

  6.   

    用表变量看效果:
    declare @ta table (用户编号 char(4), 姓名 char(1),      角色 nvarchar(10),           模块代码 char(2),   模块名称 nvarchar(10))
    insert @ta select '0000', 'A', '系统管理员', '01', '生产管理'
    union all select '0000', 'A', '系统管理员', '02', '库存管理'
    union all select '0000', 'A', '系统管理员', '03', '采购管理'
    union all select '0000', 'A', '系统管理员', '04', '销售管理'
    union all select '0000', 'A', '系统管理员', '05', '系统管理'
    union all select '0000', 'A', '操作员',     '05', '系统管理'
    delete a from @ta a where exists (select 1 from @ta where  模块名称=a.模块名称 and 模块代码=a.模块代码
    and  角色<a.角色)-->决定保留的记录select * from @ta(所影响的行数为 6 行)
    (所影响的行数为 1 行)用户编号 姓名   角色         模块代码 模块名称       
    ---- ---- ---------- ---- ---------- 
    0000 A    系统管理员      01   生产管理
    0000 A    系统管理员      02   库存管理
    0000 A    系统管理员      03   采购管理
    0000 A    系统管理员      04   销售管理
    0000 A    操作员        05   系统管理(所影响的行数为 5 行)