读书人

sql话语查询的时候能插入空行吗

发布时间: 2012-09-15 19:09:29 作者: rapoo

sql语句查询的时候能插入空行吗?
select * from table order by name

namescore
张三213
张三32
李四32
李四54
李四321
王小三2
王小三32
王小三32

我按照name分组之后,能否遇到不同的name时自动插入一个空行?

namescore
张三213
张三32

李四32
李四54
李四321

王小三2
王小三32
王小三32

[解决办法]

SQL code
if not object_id('tb') is null    drop table tbGoCreate table tb([name] nvarchar(3),[score] int)Insert tbselect N'张三',213 union allselect N'张三',32 union allselect N'李四',32 union allselect N'李四',54 union allselect N'李四',321 union allselect N'王小三',2 union allselect N'王小三',32 union allselect N'王小三',32Go;with tmp1as(select rownum=row_number()over(partition by [name] order by getdate()),       *from tb),tmp2as(select b.name,       row_number()over(partition by b.name order by (getdate()))rownum,       null col1,       null col2from master..spt_values a,(select [name],        count(*)+1 px from tb group by [Name])bwhere a.type='P' and number<b.px )select a.name,       a.Scorefrom tmp2 b left join tmp1 aon a.rownum=b.rownum and a.name=b.name/*name Score---- -----------王小三  2王小三  32王小三  32NULL NULL李四   32李四   54李四   321NULL NULL张三   213张三   32NULL NULL(11 row(s) affected)*/ 

读书人网 >SQL Server

热点推荐