过滤掉聚合行SSRS

时间:2013-03-06 12:23:55

标签: sql sql-server reporting-services

我有一个相当复杂的报道,我正在尝试编辑。以下是该报告的屏幕截图: enter image description here

我需要过滤掉“不在组中的数字”是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”。 对不起,如果这一切都没有意义,我真的需要一些帮助。

由于

1 个答案:

答案 0 :(得分: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问题时,我发现通常有助于为具有更多描述性名称的表命名,然后您可以开始查看您在哪里获得的内容,并将内容恰当地组合在一起。)