我正在准备一份SSRS报告,我只能在表格中选择访问权限。 我已经构建了查询以获取数据。但是因为我使用的是表变量,而没有在报告中获得所需的输出。
这是我的表结构:
此表存储以下列的每个作业运行数据:
jobname
instance_id--ever increasing unique number for each run
starttime
endtime
status
我想在我的报告中显示以上所有列,并且我写了如下的查询。
因为我想显示最新的运行而不管成功与否,我需要将max(instance_id)放到一个表中,并从该基表中传递所有循环。
我得到max instance_id以及用于循环目的的行号
create table ##table
(
fqjn varchar(1000),
maxi int,
rownum int
)
create table ##tb
(jobname varchar(max),
[status] varchar(100),
duration int
)
insert into ##table
select jobname,max(instance_id) as maxi,row_number() over (order by jobname desc) as rownum from [dbo].[vw_job_hist]
where grp_data_id in (select grp_Data_id from [dbo].[vw_job_data] where grp_name='pcp')
group by jobname
--now loop through table passing instance_id as parameter
declare @rownum int
select top 1 @rownum=rownum from ##table order by rownum
while @rownum is not null
begin
insert into ##tb
select jh.jobname,"job status"=
case jh.completion_status
when 0 then 'Success'
else 'Failed'
end,
"duration"=datediff(minute,jh.started_time,jh.end_time)
from [dbo].[vw_job_] jh
where jh.instance_id in (select maxi from ##table where rownum=@rownum)
set @rownum=(select top 1 rownum from ##table where rownum>@rownum
order by rownum
end
select * from ##tb
drop table ##tb
drop table ##table
我没有在SSRS中获得所需的输出,我知道如果我创建上述查询作为存储过程,我将得到所需的结果。但这是第三方数据库,我们将无法访问。
问:
任何帮助/指针都会非常感激
答案 0 :(得分:1)
试试这个:
;WITH OrderedJobInfo AS (
SELECT jobname, completion_status, started_time, end_time, instance_id
, ROW_NUMBER() OVER (PARTITION BY jobname ORDER BY instance_id DESC) AS rownum
FROM [dbo].[vw_job_hist]
WHERE grp_data_id IN (SELECT grp_Data_id FROM [dbo].[vw_job_data] WHERE grp_name='pcp')
)
SELECT o.jobname, o.instance_id
, [job status] = CASE o.completion_status WHEN 0 THEN 'Success' ELSE 'Failed' END
, [duration] = DATEDIFF(MINUTE, o.started_time, o.end_time)
FROM OrderedJobInfo o
WHERE o.rownum = 1;