简体   繁体   English

如果满足其他条件,则SSRS / T-SQL总和

[英]SSRS/T-SQL sum if another condition is met

Basically I have an application that processes transactions. 基本上我有一个处理交易的应用程序。 Each transaction has a transaction number. 每个交易都有一个交易号。 In the application view on the front the total for each transaction is calculated but it is not stored in the database (and this cannot be changed) 在前面的应用程序视图中,将计算每个事务的总计,但不会将其存储在数据库中(并且无法更改)

Now in SSRS they want to see the value come through on the report. 现在,在SSRS中,他们希望看到报告中包含的价值。 The way to do this is to match the transaction number and if they match (no matter how many records) it adds them up on is there a way to do this on SSRS? 这样做的方法是匹配交易号,如果它们匹配(无论有多少条记录),它将加起来,是否可以在SSRS上做到这一点? Or maybe a more elegant way to do it would be to do it in the stored procedure. 或者,也许更优雅的方法是在存储过程中进行。

And example of this is below which I know won't work but just to give you an idea of what I'm trying to accomplish here: 下面是一个例子,我知道这是行不通的,只是让您对我要在这里完成的事情有一个了解:

SELECT  transactionID
        , value
        , Sum(Case When transactionID = TransactionID Then Value Else 0) As Total

EDIT: In response to some of the comments I made this edit. 编辑:针对一些评论,我进行了此编辑。 First off in the above I made a mistake and did TransactionID = transaction but it should be what it is currently. 首先,在上面我犯了一个错误,并做了TransactionID = transaction,但它应该是当前状态。

Continuing on... In order to do this I need to match the transaction number against other transaction numbers in the data set and see if there is a match. 继续...为了做到这一点,我需要将交易号与数据集中的其他交易号进行匹配,看看是否存在匹配项。 To illustrate I did this example below: 为了说明,我在下面做了这个例子:

This is an example dataset: 这是一个示例数据集:

  • TransId: 1 Value: 200 TransId:1值:200
  • TransId: 2 Value: 300 TransId:2值:300
  • TransId: 1 Value: 100 TransId:1值:100
  • TransId: 2 Value: 500 TransId:2值:500
  • TransId: 1 Value: 400 TransId:1值:400

From this dataset I should get these values in the report: 从这个数据集中,我应该在报告中获得这些值:

  • TransId: 1 Value: 200 Total: 700 TransId:1值:200总计:700
  • TransId: 2 Value: 300 Total: 800 TransId:2值:300总计:800
  • TransId: 1 Value: 100 Total: 700 TransId:1值:100总计:700
  • TransId: 2 Value: 500 Total: 800 TransId:2值:500总计:800
  • TransId: 1 Value: 400 Total: 700 TransId:1值:400总计:700

So for each row I want to see the complete total for each record(TransID) and not a runnign total. 因此,对于每一行,我想查看每个记录(TransID)的完整总计,而不是不必要的总计。

This should do the trick 这应该可以解决问题

select transactionID
     , value
     , TransactionSum
 from MyTable t
  join (select transactionID 
             , sum (value) as TransactionSum 
          from MyTable
         group by transactionID
       ) x on t.transactionID = x.transactionID

Just another way of doing it 只是另一种方式

select a.transid,
       a.value,
       (select sum(b.value) from yourTable b where b.transid = a.transid) as Total
from yourTable a

Window functions are just so much more fun though: 窗口函数非常有趣:

SELECT TransactionId
    , Value
    , SUM(Value) OVER(PARTITION BY TransactionId) AS Total
FROM SomeTable

SQL Fiddle SQL小提琴

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

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