查询员工表时怎样去掉如:部门代码这些相同的内容,只让它显示一次。

解决方案 »

  1.   

    不按部门代码分组,只要求去掉这列里面重复的内容。
    如:
    emp_id     emp_name    dept_id   ....
    001         张三        A        ....
    002         李四                 .... //部门为:A
    003         王五        B        ....
    005         赵六        C        ....
    006         陈胜                 .... //部门为:C
    007         吴广                 .... //部门为:C
      

  2.   

    --注意:如果按部门编码、职工编码排序的话,新列的别名不要和原来的相同select case when emp_id=
        (select top 1 emp_id from 职工 where dept_id=A.dept_id) then dept_id else '' end as dept_id_new,
        emp_id,
        emp_name 
    from 职工 A
    order by dept_id,emp_id
      

  3.   

    declare @t table 
    (
    emp_id  varchar(5),
    emp_name  varchar(10),
    dept_id   varchar(10)
    )insert into @t 
    select '001',         '张三',        'A' union all 
    select '002',         '李四',    'A'   union all              
    select '003',         '王五',        'B' union all 
    select '005',         '赵六',        'C' union all 
    select '006',         '陈胜', 'C'  union all 
    select '007',         '吴广',  'C'
    select emp_id ,emp_name , 
    case when emp_id =
    (
    select emp_id from @t a 
    where not exists(select 1 from @t where dept_id =a.dept_id and emp_id <a.emp_id) 
    and a.emp_id =t.emp_id  ) then dept_id else '' end as [dept_id]
    from @t temp_id emp_name   dept_id    
    ------ ---------- ---------- 
    001    张三         A
    002    李四         
    003    王五         B
    005    赵六         C
    006    陈胜         
    007    吴广         (所影响的行数为 6 行)
      

  4.   

    Create  table  tb
    (
    emp_id  varchar(5),
    emp_name  varchar(10),
    dept_id   varchar(10)
    )insert into tb 
    select '001',         '张三',        'A' union all 
    select '002',         '李四',    'A'   union all              
    select '003',         '王五',        'B' union all 
    select '005',         '赵六',        'C' union all 
    select '006',         '陈胜', 'C'  union all 
    select '007',         '吴广',  'C'select *,kk=(case when a.dept_id<>isnull((select  dept_id   from  tb  where emp_id=a.emp_id-1),'') then a.dept_id else '' end)  from tb a