SSRS报告显示不同日期的数据

时间:2016-02-04 12:31:44

标签: sql date reporting-services group-by aggregate

如果我删除了sessiondate字段,我的报告(计算出勤率%)会显示汇总数据。

但我需要它,因为用户需要每月运行它(目前我们手动运行查询)

所以,我的查询中有sessiondate字段,它基于开始和结束日期参数

查询:

SELECT sessiondate,
       Possible,
       Present,
       SUM(Possible_Duration)                      AS Possible_Duration,
       SUM(Present_Duration)                       AS Present_Duration,
       CASE WHEN SUM(Present_Duration) = 0 THEN 0
       ELSE
         SUM(Present_Duration) / SUM(Possible_Duration) END AS Attendance,
       SUM(CASE WHEN Mark = 'L' THEN 1 ELSE 0 END) AS Late_Mark,
       SUM(Possible)                               AS Possib,
       SUM(CASE WHEN Mark = 'A' THEN 1 ELSE 0 END) AS Authorised_absence,
       SUM(CASE WHEN Mark = '/' THEN 1 ELSE 0 END) AS Present_,
       SUM(CASE WHEN Mark = 'O' THEN 1 ELSE 0 END) AS Absent_fromClass
  FROM Table
 WHERE Sessiondate >= @startdate
   AND Sessiondate <= @Enddate
 GROUP BY Session_dt

查询结果:

--------------------------------------
 col1 | col2 | Session_dt | attendance 
--------------------------------------
   A  |   B  | 01/01/2015 |     100
   A  |   B  | 03/01/2015 |      69
   A  |   B  | 05/01/2015 |     100

预期结果:

--------------------------
 col1 | col2 | attendance 
--------------------------
   A  |  B   |     89.3

现在我只是在查询中指定日期并运行报告。有什么方法可以使用参数值来实现这个目标吗?

2 个答案:

答案 0 :(得分:0)

此查询将在当月的第一个和最后一个日期之间返回数据,然后您只需要安排报告每月结束或运行一次:

select *
where [Date] between
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101) --first date
and 
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),101) --last date

答案 1 :(得分:0)

您可以在SSRS报告中将一个参数设置为@monthNo,只需显示Jan-Dec的月份列表,其值为1 -12。

在SQL端,您可以收到用户选择的月号,并按如下方式使用它:

declare @monthNo as char(2) = '1'; -- remove this from your SQL

declare @firstDate as datetime;
declare @lastDate as datetime;


select  @firstDate = CONVERT(datetime,CONVERT(CHAR(4),year(getdate())) + '-' + @monthNo + '-1');
select  @lastDate = dateadd(day,-1,(dateadd(month,1,@firstDate)));

select *
from [table]
where [date] between @firstDate and @lastDate;

现在您可以运行任何月份的报告。