计算给出#Error

时间:2017-10-16 11:12:41

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

我正在使用SSRS 2008r2。我需要添加一个列总数的百分比,这似乎很简单,但是我已经在这个问题上苦苦挣扎了一个星期,并且已经阅读了很多关于百分比问题的帖子,并且没有发现任何可行的帖子。

基本上我的查询是哪种方法最好 - 在报表正文或SQL代码中?然后任何人都可以帮助解决这个问题。

到目前为止我做了什么:

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

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

然后我想我可能需要在表中添加一个内连接,从而产生总行数。

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

但这只返回没有结果的标题。我认为这可能是主表和子查询表之间的链接,但我很难看到如何使计数与外部查询完全相同,而没有完全相同的代码,因为每个联系人可以有多种类型。

1 个答案:

答案 0 :(得分:1)

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

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

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