日期:2014-05-18  浏览次数:20662 次

视图查询问题
我写了个视图查询出来的数据是这样的
SQL code

张三  xxx   xxx   xxx   xxx
张三  xxx   xxx   xxx   xxx
张三  xxx   xxx   xxx   xxx
李四  xxx   xxx   xxx   xxx
李四  xxx   xxx   xxx   xxx


但是我想做出这个效果来 
SQL code

张三  xxx   xxx   xxx   xxx
      xxx   xxx   xxx   xxx
      xxx   xxx   xxx   xxx
李四  xxx   xxx   xxx   xxx
      xxx   xxx   xxx   xxx


要在sql里面实现 不在程序里面实现 可以吗

------解决方案--------------------
SQL code

--可以在视图创建的时候写,也可以在视图查询的时候写,例如张三李四这个字段是 sname

create view viewname
as
select (case when rid=1 then sname else '' end) sname,col1,col2,col3
from(
    select *,rid=row_number() over (partition by sname order by getdate())
    from tb
)t

------解决方案--------------------
SQL code

--视图查询可以直接这段,当然版本在 2005 及其上的。

select (case when rid=1 then sname else '' end) sname,col1,col2,col3
from(
    select *,rid=row_number() over (partition by sname order by getdate())
    from tb
)t

-- try do it !

------解决方案--------------------
SQL code
select
 (case px when 1 then sname else '' end) as sname,col1,col2,col3
from
 (select *,rid=row_number() over (partition by sname order by getdate())from tb)t

------解决方案--------------------
select
 (case px when 1 then sname else '' end) as sname,col1,col2,col3
from
 (select *,rid=row_number() over (partition by sname order by getdate())from tb)t