如何解决 SSRS2005 不允许的 Sum(First(...)) 限制

发布于 2024-07-16 22:12:45 字数 1123 浏览 6 评论 0原文

我遇到的问题是 SQL Server Reporting Services 不喜欢 Sum(First()) 表示法。 它只允许 Sum() 或 First()。

背景
我正在创建一份对账报告。 IE。 我们在一个时期开始时有什么袜子,订购了什么,最后我们有什么库存。

Dataset returns something like

Type,Product,Customer,Stock at Start(SAS), Ordered Qty, Stock At End (SAE)
Export,1,1,100,5,90
Export,1,2,100,5,90
Domestic,2,1,200,10,150
Domestic,2,2,200,20,150
Domestic,2,3,200,30,150

我按类型分组,然后按产品分组,并列出购买该产品的客户。 我想显示 SAS、订购数量和 SAE 的总计,但如果我对 SAS 或 SAE 进行求和,则产品 1 和 2 的值分别为 200 和 600,而其本应分别为 100 和 200。

我以为我可以做 Sum(First()) 但 SSRS 抱怨我不能在聚合中进行聚合。

理想情况下,SSRS 需要 Sum(Distinct())

迄今为止的解决方案
1. 不要将开始库存和结束库存显示为总计的一部分。
2. 直接在报表中编写一些代码来进行计算。 尝试了这个 - 没有像我预期的那样工作。 3. 编写一个程序集进行计算。 (还没有尝试过这个)

编辑 - 问题澄清
问题源于这样一个事实:这实际上是两份报告合并为一份(据我所知)。 生产报告和销售报告。
该报告试图解决这些标准:

  • 我们出售产品的市场(出口、国内)
  • 我们有多少库存、
  • 生产了多少、
  • 销售了多少、
  • 我们卖给了谁、
  • 我们还剩下多少超过。

复杂的因素是我们把它卖给了谁。 如果没有这个,事情就会相对容易一些。 但包括它意味着除了特定产品之外,其他顶线数字(开始库存和结束库存)与销售内容无关。

The problem that I have is SQL Server Reporting Services does not like Sum(First()) notation. It will only allow either Sum() or First().

The Context
I am creating a reconciliation report. ie. what sock we had a the start of a period, what was ordered and what stock we had at the end.

Dataset returns something like

Type,Product,Customer,Stock at Start(SAS), Ordered Qty, Stock At End (SAE)
Export,1,1,100,5,90
Export,1,2,100,5,90
Domestic,2,1,200,10,150
Domestic,2,2,200,20,150
Domestic,2,3,200,30,150

I group by Type, then Product and list the customers that bought that product.
I want to display the total for SAS, Ordered Qty, and SAE but if I do a Sum on the SAS or SAE I get a value of 200 and 600 for Product 1 and 2 respectively when it should have been 100 and 200 respectively.

I thought that i could do a Sum(First()) But SSRS complains that I can not have an aggregate within an aggregate.

Ideally SSRS needs a Sum(Distinct())

Solutions So Far
1. Don't show the Stock at Start and Stock At End as part of the totals.
2. Write some code directly in the report to do the calc. tried this one - didn't work as I expected.
3. Write an assembly to do the calculation. (Have not tried this one)

Edit - Problem clarification
The problem stems from the fact that this is actually two reports merged into one (as I see it). A Production Report and a sales report.
The report tried to address these criteria

  • the market that we sold it to (export, domestic)
  • how much did we have in stock,
  • how much was produced,
  • how much was sold,
  • who did we sell it to,
  • how much do we have left over.

The complicating factor is the who did we sell it to. with out that, it would have been relativly easy. But including it means that the other top line figures (stock at start and stock at end) have nothing to do with the what is sold, other than the particular product.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2024-07-23 22:12:45

我遇到了类似的问题,最终在查询中使用 ROW_NUMBER 为行值提供整数,然后使用 SUM(IIF(myRowNumber = 1, myValue, 0))。

当我开始工作并提供更多数据时,我会编辑此内容,但我认为这可能足以让您开始。 我也对阿道夫的解决方案感到好奇。

I had a similar issue and ended up using ROW_NUMBER in my query to provide a integer for the row value and then using SUM(IIF(myRowNumber = 1, myValue, 0)).

I'll edit this when I get to work and provide more data, but thought this might be enough to get you started. I'm curious about Adolf's solution too.

情愿 2024-07-23 22:12:45

呸! 我的钉子在哪里?!

您是否考虑过在 SQL 中使用窗口/排名函数来实现此目的?

这使您可以聚合数据而不会丢失详细信息

,例如 想象一下,对于一系列值,您希望返回最小值和最大值,但您也希望返回初始数据(没有数据摘要)。

Group Value Min Max
A      3    2    9
A      7    2    9
A      9    2    9
A      2    2    9
B      5    5    7
B      7    5    7
C etc..

语法看起来很奇怪,但它只是

AggregateFunctionYouWant  OVER (WhatYouWantItGroupedBy, WhatYouWantItOrderedBy) as AggVal

窗口化

排名

Pooh! Where's my peg?!

Have you thought about using windowing/ranking functions in the SQL for this?

This allows you to aggregate data without losing detail

e.g. Imagine for a range of values, you want the Min and Max returning, but you also wish to return the initial data (no summary of data).

Group Value Min Max
A      3    2    9
A      7    2    9
A      9    2    9
A      2    2    9
B      5    5    7
B      7    5    7
C etc..

Syntax looks odd but its just

AggregateFunctionYouWant  OVER (WhatYouWantItGroupedBy, WhatYouWantItOrderedBy) as AggVal

Windowing

Ranking

鼻尖触碰 2024-07-23 22:12:45

你的数据集有点奇怪,但我想我明白你要去哪里。

尝试使数据集按以下顺序返回:
类型、产品、SAS、SAE、客户、订购数量

我要做的是创建一个带有表控件的报告。 我会将类型、产品和客户设置为三个独立的组。 我会将 sas 和 sae 数据放在与产品相同的组中,并将数量放在客户组中。 这应该类似于我相信你正在努力追求的目标。 你的 sas 和 sae 应该在first()中

you're dataset is a little weird but i think i understand where you're going.

try making the dataset return in this order:
Type, Product, SAS, SAE, Customer, Ordered Qty

what i would do is create a report with a table control. i would set up the type, product, and customer as three separate groups. i would put the sas and sae data on the same group as the product, and the quantity on the customer group. this should resemble what i believe you are trying to go for. your sas and sae should be in a first()

断念 2024-07-23 22:12:45

写一个子查询。

理想情况下 SSRS 需要 Sum(Distinct())

重写您的查询才能正确执行此操作。

我怀疑你的问题是你编写的查询得到了错误的结果,或者你的表设计得很糟糕。 在不了解您要做什么的情况下,我无法告诉您如何解决它,但它有一种不好的“气味”。

Write a subquery.

Ideally SSRS needs a Sum(Distinct())

Re-write your query to do this correctly.

I suspect your problem is that you're written a query that gets you the wrong results, or you have poorly designed tables. Without knowing more about what you're trying to do, I can't tell you how to fix it, but it has a bad "smell".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文