将Reporting Services表达式转换为SQL

时间:2013-02-22 09:39:10

标签: sql sql-server reporting-services

我正在尝试修改报告,我需要根据一列中的数据排除多个行,该列的表达式为:

=iif(sum(iif(left(Fields!crs_group.Value,1) = "G",1,0)
     ,"GrpCourse")=0,"n/a",
     sum(iif(Fields!crs_group.Value="Enrolled on Course",1,0))
     - sum(iif(left(Fields!crs_group.Value,1) = "G",1,0)))

我认为如果我可以将其转换为SQL,我可以过滤掉查询中的那些行。到目前为止我的代码是:

CASE WHEN SUM(CASE WHEN LEFT(sub.crs_group, 1) = 'G' 
THEN 1 ELSE 0 END)=0 THEN 999999999
ELSE SUM(CASE WHEN sub.crs_group = 'Enrolled on Course' 
THEN 1 ELSE 0 END) - SUM(CASE WHEN LEFT(sub.crs_group,1) = 'G' 
THEN 1 ELSE 0 END)
END AS NumberNotInGroups

但是,这不会给报告运行时提供匹配的结果。我将“n / a”更改为999999999,因为我从VARCHAR转换为INT错误。我已经尝试过转换到VARCHAR但没有成功。任何帮助都会非常感激,昨天整个下午都在努力解决这个问题。

修改

以下是完整查询:

    SELECT        
sub.course, 
sub.crs_group,
m.m_reference, 
me.e_status, 
me.e_id,
s.s_studentreference,
p.p_forenames,
p.p_surname,
pcd.p_surname + ',' + pcd.p_forenames as course_dir,
CASE WHEN SUM(CASE WHEN LEFT(sub.crs_group, 1) = 'G' THEN 1 ELSE 0 END)=0 THEN 999999999
     ELSE SUM(CASE WHEN sub.crs_group = 'Enrolled on Course' THEN 1 ELSE 0 END) - SUM(CASE WHEN LEFT(sub.crs_group,1) = 'G' THEN 1 ELSE 0 END)
END AS NumberNotInGroups



FROM            
msql.unitesnapshot.dbo.capd_moduleenrolment AS me 
INNER JOIN msql.unitesnapshot.dbo.capd_module AS m ON me.e_module = m.m_id
LEFT JOIN msql.unitesnapshot.dbo.capd_staff scd on m.m_modulesupervisor = scd.s_id
LEFT JOIN msql.unitesnapshot.dbo.capd_person pcd on scd.s_id = pcd.p_id
INNER JOIN msql.unitesnapshot.dbo.capd_student s on me.e_student = s.s_id
INNER JOIN msql.unitesnapshot.dbo.capd_person p on s.s_id = p.p_id
INNER JOIN (SELECT

            m.m_id,
            CASE WHEN m.m_reference not like '%G_' THEN m.m_reference ELSE LEFT(m.m_reference, charindex('G', m.m_reference) - 1) END AS course, 
            CASE WHEN m.m_reference not like '%G_' THEN 'Enrolled on Course' ELSE RIGHT(m.m_reference, 2) END AS crs_group

            FROM

            unitesnapshot.dbo.capd_module m) sub ON sub.m_id = me.e_module

WHERE        
me.e_status = 'A' AND 
LEFT(m.m_reference, 2) = '12' AND 
SUBSTRING(m.m_reference, 7, 2) in ('VF','AB','FB')

GROUP BY

sub.course, 
sub.crs_group,
m.m_reference, 
me.e_status, 
me.e_id,
s.s_studentreference,
p.p_forenames,
p.p_surname,
pcd.p_surname + ',' + pcd.p_forenames

ORDER BY 
p.p_surname

以下是SSRS中的正确结果:

enter image description here

这是sql-server中的错误结果,它应该显示1而不是99999999:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可能需要更好地解释问题并显示查询的更多详细信息。看起来你正试图展示那些人们注册的不是小组的课程。但是,您使用相同的字段crs_Group来显示它们是仅仅是已注册还是属于某个组。现在,如果crs_Group等于“已注册课程”,但是当他们加入某个小组时会更改为以G开头的内容,那么当课程中有更多人在群组中时,您的等式应该给出负数。

所以也许最好计算一个课程中注册的每个人,而不是那些群体中的人最终选择课程中的人而不是小组:

SELECT CourseName, 
  COUNT(*) - SUM(CASE WHEN LEFT(crs_group, 1) = 'G' THEN 1 END) AS NumberNotInGroups
FROM Courses
GROUP BY CourseName
HAVING COUNT(*) - SUM(CASE WHEN LEFT(crs_group, 1) = 'G' THEN 1 ELSE 0 END) <> 0

此查询会过滤掉每个人都在群组中的课程。