简体   繁体   English

筛选出汇总的行SSRS

[英]Filter out aggregated rows SSRS

I have a fairly complicated report that I'm trying to edit. 我有一个要编辑的相当复杂的报告。 Below is a screenshot of the report: 以下是该报告的屏幕截图: 在此处输入图片说明

I need to filter out all rows where "Number not in Groups" is n/a or 0, I've tried filtering out using the filter option in tablix properties, but it wont let me due to the error: aggregate functions cannot be used in dataset filters. 我需要过滤掉“不在组中的数字”为n / a或0的所有行,我尝试使用tablix属性中的过滤器选项进行过滤,但是由于以下错误,我无法通过它:无法使用聚合函数在数据集过滤器中。

Below is the code for the expression that makes up the field "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)))

I have also tried to filter out those rows in the SQL query but haven't had much luck. 我也曾尝试过滤掉SQL查询中的那些行,但是运气不佳。 Below is my attempt at filtering out the rows in the query: 以下是我尝试过滤掉查询中的行的尝试:

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

However this doesn't work because it includes rows that would have the n/a value in the report. 但是,这行不通,因为它包含在报表中具有n / a值的行。 A group is defined by a course code that ends in G_, however some course's don't have groups so these appear as 'n/a'. 组由以G_结尾的课程代码定义,但是某些课程没有组,因此它们显示为“ n / a”。 Sorry if none of this makes sense, I just really need some help with it. 抱歉,如果所有这些都没有意义,我真的需要一些帮助。

Thanks 谢谢

I haven't entirely understood your query, but seems like you could do this in the query with a Common Table Expression (CTE): 我还没有完全理解您的查询,但是似乎您可以使用通用表表达式(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)

(When struggling with a SQL problem, I find it often helps to name tables with more descriptive names, then you can start to see what you've got where, and piece things together appropriately.) (在处理SQL问题时,我发现使用描述性更强的名称来命名表通常会有所帮助,然后您可以开始查看所拥有的内容,并将它们适当地组合在一起。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM