计算总百分比(每行)给出#Error

Calculation for Percentage of total (per row) giving #Error

我正在使用 SSRS 2008r2。我需要为总数的百分比添加一列,这看起来很简单,但是我已经为此苦苦挣扎了一个星期,并且阅读了很多关于百分比问题的帖子,但没有找到任何有用的东西。

基本上,我的查询是在报告 body 中还是在 SQL 代码中执行此操作的最佳方法?然后任何人都可以帮助如何让它工作。

到目前为止我做了什么:

在 SSRS 中,我在结束列中添加了一个表达式,以将每种类型的记录数除以总行中的值: =Fields!WebCommsPrefContact.Value/ReportItems!WebCommsPrefContact1 我在 'Text Box Properties' 中将表达式单元格格式化为百分比格式。但是结果显示错误。我已经阅读了很多关于此的帖子,但它们似乎都指的是除以零,但总行永远不会包含零。

然后我想也许我需要将百分比作为一列添加到我的 SQL 代码中: 原代码:

SELECT DISTINCT cp.tsg_communicationpreferencetypeidname AS WebCommsPrefType, con.tsg_contactuid AS WebCommsPrefContact
FROM            Filteredccx_communicationpreference AS cp INNER JOIN
                         FilteredContact AS con ON cp.ccx_contact = con.contactid INNER JOIN
                         FilteredAccount AS comp ON con.accountid = comp.accountid
WHERE        (cp.ccx_informationsource = 803080004) AND (con.statecode = 0) AND (cp.createdon BETWEEN '2016/11/25' AND '2017/10/13') AND (NOT EXISTS
                             (SELECT        tsg_companyuid
                               FROM            FilteredAccount
                               WHERE        (comp.tsg_companyuid IN ('COMP00153968', 'COMP00091748', 'COMP00177586', 'COMP00231427', 'COMP00077428', 'COMP00077490', 'COMP00255796')))) AND 
                         (cp.tsg_communicationpreferencecategoryidname LIKE 'Category 4%') AND (cp.ccx_status = 803080000)
ORDER BY WebCommsPrefType

我试图通过在 select 行中添加一个新的子查询来添加一个新列,但这导致全为零:(在此摘录中,我仍在尝试获取总行数,所以没有还添加在除法部分以获得百分比结果)-语法显然不正确

SELECT DISTINCT cp.tsg_communicationpreferencetypeidname AS WebCommsPrefType, con.tsg_contactuid AS WebCommsPrefContact, 
count(select Filteredccx_communicationpreference.tsg_communicationpreferencetypeidname AS WebCommsPrefType, FilteredContact.tsg_contactuid AS WebCommsPrefContact
                        FROM            Filteredccx_communicationpreference  INNER JOIN
                                                 FilteredContact ON   Filteredccx_communicationpreference.ccx_contact = FilteredContact.contactid INNER JOIN
                                                 FilteredAccount  ON  FilteredContact.accountid = FilteredAccount.accountid
                        WHERE        (  Filteredccx_communicationpreference.ccx_informationsource = 803080004) AND ( FilteredContact.statecode = 0) AND (Filteredccx_communicationpreference.createdon BETWEEN '2016/11/25' AND '2017/10/13') AND (NOT EXISTS
                                                     (SELECT        tsg_companyuid
                                                       FROM            FilteredAccount
                                                       WHERE        (FilteredAccount.tsg_companyuid IN ('COMP00153968', 'COMP00091748', 'COMP00177586', 'COMP00231427', 'COMP00077428', 'COMP00077490', 'COMP00255796')))) AND 
                                                 (  Filteredccx_communicationpreference.tsg_communicationpreferencecategoryidname LIKE 'Category 4%') AND (Filteredccx_communicationpreference.ccx_status = 803080000)) as RecordCount


From 
                    Filteredccx_communicationpreference AS cp INNER JOIN
                        FilteredContact AS con ON cp.ccx_contact = con.contactid INNER JOIN
                        FilteredAccount AS comp ON con.accountid = comp.accountid
WHERE        (cp.ccx_informationsource = 803080004) AND (con.statecode = 0) AND (cp.createdon BETWEEN '2016/11/25' AND '2017/10/13') AND (NOT EXISTS
                            (SELECT        tsg_companyuid
                            FROM            FilteredAccount
                            WHERE        (comp.tsg_companyuid IN ('COMP00153968', 'COMP00091748', 'COMP00177586', 'COMP00231427', 'COMP00077428', 'COMP00077490', 'COMP00255796')))) AND 
                        (cp.tsg_communicationpreferencecategoryidname LIKE 'Category 4%') AND (cp.ccx_status = 803080000)

然后我想也许我需要向 table 添加一个内部联接,从而得到总行数。

SELECT DISTINCT cp.tsg_communicationpreferencetypeidname AS WebCommsPrefType, count(con.tsg_contactuid) AS WebCommsPrefContact, count(con.tsg_contactuid)/count(X.PContact) as percentage
FROM            Filteredccx_communicationpreference AS cp INNER JOIN
                         FilteredContact AS con ON cp.ccx_contact = con.contactid INNER JOIN
                         FilteredAccount AS comp ON con.accountid = comp.accountid Inner join
                         (select Distinct Filteredccx_communicationpreference.tsg_communicationpreferencetypeidname AS WebCommsPrefType, FilteredContact.tsg_contactuid as PContact
                         FROM            Filteredccx_communicationpreference  INNER JOIN
                         FilteredContact  ON Filteredccx_communicationpreference.ccx_contact = FilteredContact .contactid INNER JOIN
                         FilteredAccount ON FilteredContact.accountid = FilteredAccount.accountid
                            WHERE        (Filteredccx_communicationpreference.ccx_informationsource = 803080004) AND (FilteredContact.statecode = 0) AND (Filteredccx_communicationpreference.createdon BETWEEN '2016/11/25' AND '2017/10/16') AND (NOT EXISTS
                             (SELECT        tsg_companyuid
                               FROM            FilteredAccount
                               WHERE        (FilteredAccount.tsg_companyuid IN ('COMP00153968', 'COMP00091748', 'COMP00177586', 'COMP00231427', 'COMP00077428', 'COMP00077490', 'COMP00255796')))) AND 
                         (Filteredccx_communicationpreference.tsg_communicationpreferencecategoryidname LIKE 'Category 4%') AND (Filteredccx_communicationpreference.ccx_status = 803080000)) as X ON cp.ccx_contact=X.PContact

WHERE        (cp.ccx_informationsource = 803080004) AND (con.statecode = 0) AND (cp.createdon BETWEEN '2016/11/25' AND '2017/10/16') AND (NOT EXISTS
                             (SELECT        tsg_companyuid
                               FROM            FilteredAccount
                               WHERE        (comp.tsg_companyuid IN ('COMP00153968', 'COMP00091748', 'COMP00177586', 'COMP00231427', 'COMP00077428', 'COMP00077490', 'COMP00255796')))) AND 
                         (cp.tsg_communicationpreferencecategoryidname LIKE 'Category 4%') AND (cp.ccx_status = 803080000)
                         Group By  cp.tsg_communicationpreferencetypeidname
ORDER BY WebCommsPrefType

但这只是returns headers 没有结果。我认为这可能是主要 table 和 sub-query table 之间的 link 但我正在努力了解如何获得与外部完全相同的计数无需完全相同的代码即可查询,因为每个联系人可以有多种类型。

今天早上我不那么累的时候再看看我的问题,答案很简单。我遗漏了表达式的 .value 部分,不需要继续尝试在我的 SQL 代码中找到完美答案。简单易行的答案是使用以下表达式在 SSRS 报告正文中计算百分比:

=Fields!WebCommsPrefContact.Value/ReportItems!WebCommsPrefContact1.Value

然后将文本框格式化为百分比。