我有一个查询,我总是希望返回10条记录:
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
答案 0 :(得分:4)
count()
永远不会返回小于零...所以只需在计数列中通过union附加10个带有-1的虚拟行
此外,请勿使用SET ROWCOUNT,因为它会影响中间结果
SELECT TOP 10
row_number() over(order by TheCount desc) AS row_num,
hist_report_id,
report_name
FROM
(
select
,count(*) AS TheCount
,hist_report_id
,max(rpt_report_name) AS 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
UNION ALL
SELECT TOP 10
-1, NULL, NULL
FROM sys.columns
) T
答案 1 :(得分:1)
with
/* Create 10 dummy rows using recursion */
RowSequence as (
select 0 Idx
union all
select RowSequence.Idx+1 Idx from RowSequence where RowSequence.Idx+1<10
),
/* Your Query */
YourQuery as(
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
)
/* Now all together */
select top 10 * from (
select * from YourQuery
union all
select null, null, null from RowSequence
) a
/* To avoid the recursion maximum level 100 */
option (maxrecursion 0)
答案 2 :(得分:0)
我记得最简单的方式:
Insert
select result
#temptable
While count(*) < 10
insert
行into #temptable
答案 3 :(得分:0)
您可以考虑使用TVF为您返回一组您可以加入的数字:
CREATE FUNCTION SetOfValues (@beginningAt int, @endingAt int, @step int = 1)
RETURNS
@result TABLE (value int )
AS BEGIN
declare @counter int set @counter = @beginningAt
while(@counter<=@endingAt)
begin
insert into @result values(@counter)
set @counter = @counter + @step
end
RETURN
END
GO
-- USAGE
with cte as (
select row_number() over (order by hist_report_Id) as row_num,
hist_report_Id, report_name
from tbl_report_history join tbl_report_definitions on hist_report_Id = rpt_reportId)
select value, hist_reportId, report_name
from dbo.SetOfValues(1,10,1) as t on t.value = cte.row_num