[英]SSRS Report shows data for different dates
My report(which calculates attendance %) shows aggregated data if I remove the sessiondate field. 如果我删除sessiondate字段,我的报告(计算出勤百分比)将显示汇总数据。
But I need it as users need to run it each month(currently we run query manually) 但是我需要它,因为用户需要每月运行它(当前我们手动运行查询)
So, I have sessiondate field in my query which is based on start and end date parameters 因此,我的查询中有基于开始和结束日期参数的sessiondate字段
Query: 查询:
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
Query result: 查询结果:
--------------------------------------
col1 | col2 | Session_dt | attendance
--------------------------------------
A | B | 01/01/2015 | 100
A | B | 03/01/2015 | 69
A | B | 05/01/2015 | 100
Expected result: 预期结果:
--------------------------
col1 | col2 | attendance
--------------------------
A | B | 89.3
For now I just specified dates in my query and running the report. 现在,我只在查询中指定日期并运行报告。 Is there any way I can achieve this using parameter values?
有什么办法可以使用参数值来实现此目的?
This query will return data between first and last date of the current month, then you only need to schedule your report to run every month end or something: 该查询将返回当月第一天和最后一个日期之间的数据,然后您只需要计划报表在每个月末运行即可:
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
You can put one parameter on your SSRS report as @monthNo and simply display the month list of Jan - Dec with simply value of 1 -12. 您可以将一个参数作为@monthNo放在您的SSRS报告中,并仅显示1月-12月的月份列表,其值仅为1 -12。
On SQL side, you could receive month number that the user selected and use it like this: 在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;
Now you can run the report for whatever month. 现在您可以在任何月份运行报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.