对视图执行操作

时间:2014-05-20 11:07:44

标签: sql sql-server sql-server-2008

我创建了一个view T3

cliid     fullname   MgrName    Mgrid
  12          V1         Vas       1
  13          V2         xyz       2
  14          V3         abc       4
  15          V4         Vas       1
  16          V5         Vas       1

和另一个view as T4

 AckNo     clifullname   
  1222          V1         
  1333          V2        
  2334          V3         
  1533          V4         
  1600          V5   

我想对这些视图执行连接以产生如下结果:

 AckNo      cliid     fullname   MgrName    Mgrid
  1222        12          V1         Vas       1
  1333        13          V2         xyz       2
  2334        14          V3         abc       4
  1533        15          V4         Vas       1
  1600        16          V5         Vas       1

到目前为止,我已经尝试了以下查询,但是我想要解决这个问题的重复记录。

  select T4.AckNo,T3.cliid,T3.FullName,T3.MgrName,T3.MgrId
  from T3
  join T4 On T4.cliFullName=T3.FullName 

1 个答案:

答案 0 :(得分:1)

  select AckNo,cliid,fullname,MgrName,Mgrid from
  (
  select *,rn=ROW_NUMBER()over(order by cliid) from T3 --your view
  )x,
  (
  select *,rn1=ROW_NUMBER()over(order by AckNo) from T4 --your view
  )y
 where x.rn=y.rn1

SEE DEMO

//你可以像这样创建视图

 create view tiger as
 select AckNo,cliid,fullname,MgrName,Mgrid from
  (
  select *,rn=ROW_NUMBER()over(order by cliid) from tabl1 --your view(T3)
  )x,
  (
  select *,rn1=ROW_NUMBER()over(order by AckNo) from table2 --your view(T4)
  )y
 where x.rn=y.rn1

 select * from tiger