简体   繁体   English

手动汇总或汇总SSRS中的数据字段

[英]manually Aggregate or Sum data fields in groups in SSRS

I have a Sales Table and a Discounts table. 我有一个销售表和一个折扣表。

The main dataset is a Sales table has sales per person. 主要数据集是一个Sales表,其中包含每人的销售额。

Every person is associated under only one project and every project has only one supervisor. 每个人仅在一个项目下关联,每个项目只有一名主管。

The second dataset - discounts table has the discount entry for each Project 第二个数据集-折扣表具有每个项目的折扣条目

Sales Table

Supervisor  | Company   |Project    |Person |Sale
Adam        | CompanyA  |CompA-01   |Amy    |100
Adam        | CompanyA  |CompA-02   |Beth   |200
Adam        | CompanyA  |CompA-02   |Cary   |200
Jenny       | CompanyA  |CompA-05   |Dawn   |300
Jenny       | CompanyA  |CompA-05   |Emma   |400



Discounts table

Company | Project   |Month  |Amount
CompanyA| CompA-02  |Jan    |500
CompanyA| CompA-02  |Feb    |500
CompanyA| CompA-05  |Jan    |1000

I want to display a report where the Discounts field is calculated as below 我想显示一个如下所示的“折扣”字段的报告

Supervisor  | Company   |Project    |Person |Sales  |Discounts 
Adam                            
          CompanyA                           500       1000 <---HELP CALCULATE THIS
                         CompA-01            100       0
                                      Amy    100
                         CompA-02            400       1000
                                      Beth   200
                                      Cary   200
...
...
TOTAL                                        12000     2000                                                            

I have following row groups in my report: 我的报告中包含以下行组:

Supervisor Group
---Company Group
-----Project Group
----------Details

I am able to calculate the Discount column at the Project Grouping level as follows 我可以按以下方式在“项目分组”级别计算“折扣”列

=Code.SumLookup(LookupSet(Fields!Project.Value,Fields!Project.Value,Fields!amount.Value,"Discounts"))

Then at the Total Level I calculate the discount as: 然后在总级别上,我将折扣计算为:

=Sum(Fields!amount.Value, "Discounts")

I need help figuring out how to calculate the sum of the discounts at the Company and Supervisor level. 我需要帮助弄清楚如何计算公司和主管级别的折扣总额。


As many of you suggested that my query was the problem I just wanted to go ahead and demo exactly what issues I am facing. 正如你们中许多人所建议的那样,我的问题只是我要继续进行下去并演示我所面临的问题的问题。

As suggested I redesigned the query using a join 按照建议,我使用联接重新设计了查询

SELECT Sales.Supervisor, Sales.Company, Sales.Project, Sales.Person, Sales.SaleAmt, SUM(Discounts.Amount)
FROM Sales 
LEFT OUTER JOIN Discounts 
ON Sales.Company = Discounts.Company 
AND Sales.Project = Discounts.Project 
group by Sales.Supervisor, Sales.Company, Sales.Project, Sales.Person, Sales.SaleAmt, Discounts.Amount

created a report like 创建了一个报告,例如

在此处输入图片说明

The report previews like this 该报告预览如下 在此处输入图片说明

Notice how the discount values are summed up all wrong. 请注意,折扣值的总和是错误的。 What can I do to correct this? 我该怎么做才能纠正这个问题?

You are making too much work for yourself IMHO having the code do it. 恕我直言,您正在为代码做太多的工作。 SSRS is built in with an understanding of the level in the grouping you are on. 内置SSRS时会了解您所在分组的级别。 EG: If I have a detail level with a function like Sum(Discount), then I add a 'Project' grouping and choose to add a 'header' row. EG:如果我有一个带有Sum(Discount)之类功能的明细级别,那么我将添加一个“项目”分组并选择添加一个“标题”行。 If I put that calculation in the new row above the 'Detail' row you now are grouping by the other data. 如果我将计算结果放在“详细信息”行上方的新行中,那么您现在将按其他数据分组。 You can then group the projects by Company, and the Companies by Supervisor, etc.. You can then add the same calculation on a different row and it will achieve different values merely by knowing the level it is on. 然后,您可以按公司将项目分组,按主管将公司分组,依此类推。然后,您可以在不同的行上添加相同的计算,仅通过了解其所在的级别即可获得不同的值。

Rather than writing complex expressions or nested queries to join these two datasets, it would be easier to add a separate dataset that gets the data you need for a lookup. 与其编写复杂的表达式或嵌套查询以连接这两个数据集,不如添加一个单独的数据集来获取查找所需的数据,这将更加容易。 For example, add one dataset called CompanyDiscounts which would look like this: 例如,添加一个名为CompanyDiscounts的数据集,如下所示:

select Company, sum(Amount) as Discount
from DiscountsTable

Now you can add a lookup function to get the total discount at the Company level. 现在,您可以添加查找功能,以在公司级别获得总折扣。

=Lookup(Fields!Company.Value, Fields!Company.Value, Fields!Discount.Value, "CompanyDiscounts")

You can repeat these steps for the Suporvisor level. 您可以对Suporvisor级别重复这些步骤。

Normally I wouldn't recommend a method like this, but the nature of your data structure lends itself to this sort of workaround. 通常,我不会推荐这样的方法,但是您的数据结构的性质适合于这种解决方法。 Ideally, you would have a sale ID with an associated discount so that you can join all the relevant data in one query and just use grouping aggregates to fill in the table. 理想情况下,您将拥有一个带有折扣的销售ID,以便您可以将所有相关数据合并到一个查询中,而仅使用分组汇总来填充表格。

Please use below query 请使用以下查询

SELECT Sales.Supervisor, 
       Sales.Company, 
       Sales.Project, 
       Sales.Person, 
       Sales.Sale, 
       Discounts.Amount
FROM  Sales 
LEFT OUTER JOIN Discounts ON Sales.Company = Discounts.Company 
                          AND Sales.Project = Discounts.Project
GROUP BY Sales.Supervisor, 
         Sales.Company, 
         Sales.Project, 
         Sales.Person, 
         Sales.Sale, 
         Discounts.Amount

in the data set and with the help of Tabular mode you will able to generate the report. 在数据集中并借助表格模式,您将能够生成报告。 I have tried it and it worked. 我已经尝试过了,而且效果很好。

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

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