指定数量的我有一个查询,我一直想为返回10个记录:总是返回记录SQL服务器
set rowcount 10
select row_number() over(order by count(*) desc) row_num
,hist_report_id
,max(rpt_report_name) report_name
from utility.dbo.tbl_report_history
join utility.dbo.tbl_report_definitions
on hist_report_id = rpt_report_id
where hist_user_id = 1038
group by hist_report_id
,如果我有10分或更多的记录,工作正常。问题是当记录少于10条时,我仍然需要在report_id和report_name字段中返回带有空值的rownumber字段。
如果只有7条记录返回,结果应该是这样的:
row_num report_id report_name
1 id1 name1
2 id2 name2
3 id3 name3
4 id4 name4
5 id5 name5
6 id6 name6
7 id7 name7
8 null null
9 null null
10 null null
有什么建议?
我使用SQL Server 2008的
SQL Server版本?我想你需要某种与临时表或表变量的联合。 – Blorgbeard 2012-01-30 14:40:15