If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([ID] int,[P_TITLE] nvarchar(6),[P_COMPANY] nvarchar(3),[P_TIME] Datetime)
Insert tb
Select 1,'aaaaaa','公司1','2009-08-05' union all
Select 2,'bbbbbb','公司1','2009-08-05' union all
Select 3,'vvvvvv','公司1','2009-08-05' union all
Select 4,'cccccc','公司2','2009-08-05' union all
Select 5,'dddddd','公司2','2009-08-05' union all
Select 6,'eeeeee','公司2','2009-08-05' union all
Select 7,'ffffff','公司3','2009-08-05' union all
Select 8,'gggggg','公司3','2009-08-05' union all
Select 9,'hhhhhh','公司3','2009-08-05'
Go
--Select * from tb
-->SQL查询如下:
;with t as
(
select rn=(row_number()over(order by id)-1)/5,*,px=0
from tb
union all
select (id-1)/5,null,null,null,null,1
from tb
group by (id-1)/5
)
select [ID],[P_TITLE],[P_COMPANY],[P_TIME]
from t
order by rn,px
/*
ID P_TITLE P_COMPANY P_TIME
----------- ------- --------- -----------------------
1 aaaaaa 公司1 2009-08-05 00:00:00.000
2 bbbbbb 公司1 2009-08-05 00:00:00.000
3 vvvvvv 公司1 2009-08-05 00:00:00.000
4 cccccc 公司2 2009-08-05 00:00:00.000
5 dddddd 公司2 2009-08-05 00:00:00.000
NULL NULL NULL NULL
6 eeeeee 公司2 2009-08-05 00:00:00.000
7 ffffff 公司3 2009-08-05 00:00:00.000
8 gggggg 公司3 2009-08-05 00:00:00.000
9 hhhhhh 公司3 2009-08-05 00:00:00.000
NULL NULL NULL NULL
(11 行受影响)
*/