简体   繁体   English

将Reporting Services表达式转换为SQL

[英]Convert Reporting Services expression to SQL

I'm trying to modify a report and I need to exclude a number of rows based on the data in one column the expression for that column is: 我正在尝试修改报告,并且需要根据一列中的数据排除许多行,该列的表达式为:

=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 think if I can convert it to SQL I can filter out those rows in the query. 我认为如果可以将其转换为SQL,则可以过滤掉查询中的那些行。 My code so far is: 到目前为止,我的代码是:

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

However this doesn't give matching results to when the report is run. 但是,这与运行报表时的结果不符。 I've change "n/a" to 999999999 because I get a conversion from VARCHAR to INT error. 我将“ n / a”更改为999999999,因为我从VARCHAR转换为INT错误。 I've tried casting to VARCHAR with no success. 我尝试将其转换为VARCHAR失败。 Any help would be much appreciated, spent all afternoon yesterday trying to figure it out. 任何帮助将不胜感激,昨天整个下午都在努力解决问题。

EDIT 编辑

Here is the full 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,
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

Here is the correct result in SSRS: 这是SSRS中的正确结果:

在此处输入图片说明

And here is the incorrect result in sql-server, it should show 1 not 99999999: 这是sql-server中的错误结果,它应该显示1而不是99999999:

在此处输入图片说明

You probably need to explain the question better and show more details of your queries. 您可能需要更好地解释问题,并显示更多查询细节。 It looks like you're trying to show those courses which have people enrolled who aren't in groups. 看来您正在尝试显示那些招募但未分组的人的课程。 However, you use the same field, crs_Group , to show whether they are simply enrolled or whether they are in a group. 但是,您使用相同的字段crs_Group来显示它们是否只是被注册或是否在组中。 Now, if crs_Group equals "Enrolled on Course" but changes to something beginning with G when they join a group, then your equation should give a negative number when the course has more people in groups than not. 现在,如果crs_Group等于“已crs_Group参加课程”,但当他们加入一个组时变为以G开头的内容,那么当课程中有更多的人时,方程式应该给出一个负数。

So maybe it would be better to calculate everyone enrolled in a course less those people in groups to end up with the people on the course but not in groups: 因此,也许最好计算出参加课程的每个人的人数,而不是小组人数,而不是小组人数:

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

This query filters out courses where everyone is in a group. 该查询将筛选出每个人都在小组中的课程。

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

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