0

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

=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 中的正确结果:

在此处输入图像描述

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

在此处输入图像描述

4

1 回答 1

1

您可能需要更好地解释问题并显示查询的更多详细信息。您似乎正在尝试显示那些注册了未分组的人的课程。但是,您使用相同的字段 ,crs_Group来显示他们是简单注册还是在一个组中。现在,如果crs_Group等于“Enrolled on Course”但在他们加入小组时更改为以 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

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

于 2013-02-22T10:46:12.320 回答