select a.sid, a.person, a.name,b.dept_title
from view_REC_JUDGE a, VIEW_hum_dept_hum b  where rec_type ='0468' and a.name = b.name and rec_sid=2
order by a.advicedate
这段代码查询出来是这个结果:
sid         person      name                 dept_title
----------- ----------- -----------------------------------
2197        1           系统管理员            陕西化建工程有限责任公司
2198        200         苏磊                   信息中心
2198        200         苏磊                   陕西化建工程有限责任公司
2199        907         张信                   信息中心
2199        907         张信                   陕西化建工程有限责任公司
2199        907         张信                   测试项目
现在我想把查询出来的结果再过滤下  变成这个样子的  ,请问怎么写sql啊sid         person      name                 dept_title
----------- ----------- -----------------------------------
2197        1           系统管理员            陕西化建工程有限责任公司
2198        200         苏磊                   信息中心
2199        907         张信                   信息中心

解决方案 »

  1.   


    select sid,person,name,dept_title from (
    select row_number() over (partition by a.sid order by a.advicedate) as rowid,
    a.sid as rowid, a.person, a.name,b.dept_title
    from view_REC_JUDGE a, VIEW_hum_dept_hum b 
    where rec_type ='0468' and a.name = b.name and rec_sid=2
    ) aa where rowid=1
      

  2.   

    select 
    *
    from (select a.sid, a.person, a.name,b.dept_title,Row=row_number()over(partition by a.sid, a.person, a.name order by len(b.dept_title))
    from view_REC_JUDGE a, VIEW_hum_dept_hum b where rec_type ='0468' and a.name = b.name and rec_sid=2
    )t
    where Row=1
      

  3.   

         distinct后 用left join on