【问题标题】:Calculation for Percentage of total (per row) giving #Error计算总百分比(每行)给出#Error
【发布时间】:2018-03-27 21:29:35
【问题描述】:

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

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

到目前为止我做了什么:

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

然后我想也许我需要将百分比作为一列添加到我的 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 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)

然后我想也许我需要向一个表添加一个内部连接,从而产生总行数。

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

但这只会返回没有结果的标题。我认为这可能是主表和子查询表之间的链接,但我正在努力了解如何在没有完全相同的代码的情况下获得与外部查询完全相同的计数,因为每个联系人可以有多种类型。

【问题讨论】:

    标签: sql-server reporting-services ssrs-2008-r2 percentage


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      相关资源
      最近更新 更多