0

我正在尝试编辑一份相当复杂的报告。以下是报告截图: 在此处输入图像描述

我需要过滤掉“不在组中的数字”为 n/a 或 0 的所有行,我尝试使用 tablix 属性中的过滤器选项过滤掉,但由于错误它不会让我:无法使用聚合函数在数据集过滤器中。

下面是构成“Number not in Groups”字段的表达式的代码:

=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 查询中的那些行,但运气不佳。下面是我尝试过滤掉查询中的行:

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 ,
    ISNULL(COUNT(*) - SUM(CASE WHEN LEFT(crs_group, 1) = 'G' THEN 1
                          END), 1) 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
HAVING
    COUNT(*) - SUM(CASE WHEN LEFT(crs_group, 1) = 'G' THEN 1
                        ELSE 0
                   END) <> 0
ORDER BY
    p.p_surname ,
    p.p_forenames

但是,这不起作用,因为它包含在报告中具有 n/a 值的行。小组由以 G_ 结尾的课程代码定义,但是有些课程没有小组,因此这些课程显示为“n/a”。对不起,如果这些都没有意义,我真的需要一些帮助。

谢谢

4

1 回答 1

1

我还没有完全理解您的查询,但似乎您可以使用公用表表达式 (CTE) 在查询中执行此操作:

;
WITH 
CourseIsGroupCourse
AS
(
   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 ,
      SUM(...[Something I haven't figured out yet...]) as IsGroupCourse
   FROM
      unitesnapshot.dbo.capd_module m
   GROUP BY
      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 
)
[Now back to your big query...]

SELECT
    sub.course ,
    sub.crs_group ,
    m.m_reference ,
   ...
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 
...
WHERE
    me.e_status = 'A'
    AND LEFT(m.m_reference, 2) = '12'
    AND SUBSTRING(m.m_reference, 7, 2) IN ( 'VF', 'AB', 'FB' )
-- Here's the key bit...
    AND sub.course in (SELECT course FROM CourseIsGroupCourse where IsGroupCourse = 1)

(在处理 SQL 问题时,我发现用更具描述性的名称命名表通常会有所帮助,然后您就可以开始查看您在哪里得到了什么,并适当地将它们拼凑在一起。)

于 2013-03-06T15:08:28.947 回答