SQL Server Reporting Services Running Total Over Aggregated Data - reporting-services

Everyone,
In SSRS, we have 2 columns as laid out below.
Sales | Running Sales
5.00 | 5.00
3.00 | 8.00
1.00 | 9.00
The distinction is that the first column (sales) is a is a grouping row and thus to get a Total for Sales per row, we are using =Sum(Fields!Sales.Value).
The problem occurs that when I try to use running value to get a running sales total. It gives me the SSRS error that Aggregate functions can only be used on page headers and footers. In this case it makes no sense to have the total in the footer. Does anyone know a solution/workaround to this problem.
Thanks.

I had the same problem; here is how I solved it.
So here is how to subtotal a column that is itself a sum function. SSRS 2005 wont allow you to aggregate an aggregate function. For example, the total of a column showing a Running Total, useful in daily stock balance calculations.
Add the following code to the report Report > Properties
Dim public totalBalance As Decimal
Public Function AddTotal(ByVal balance As Decimal) AS Decimal totalBalance = totalBalance + balance return balance
End Function
Public Function GetTotal() return totalBalance
End Function
This code adds two variables: totalbalance and cnt as decimal numbers. And two functions AddTotal and GetTotal.
AddTotal allows items in rows to be added up, use as follows in a value cell, where you had;
=RunningTotal(Fields!ColumnName.Value,sum,nothing)
with
=Code.AddTotal(RunningTotal(Fields!ColumnName.Value,sum,nothing))
in the total cell, where it you were unable to simply use
=sum(RunningTotal(Fields!ColumnName.Value,sum,nothing))
use instead
=Code.GetTotal()
Simply add more variables and public functions if you need to sum the sum of more that one field.
http://blog.wingateuk.com/2011/09/ssrs-aggregate-of-aggregate.html

Related

SQL query to get the average time spent by user on page

Here is a sample table that I am using,
User_id timestamp action
1 2020-10-01 09:00:00 Opened page
1 2020-10-01 09:10:00 Closed page
2 2020-10-02 04:00:00 Signed up
3 2020-10-02 06:00:00 Opened page
3 2020-10-03 11:00:00 Made a booking
3 2020-10-03 09:30:00 Closed page
need to write a SQL query to find the average time spent by a user on the page.
The expected answer is just a number which represents the average time spent by an average user on the page.
You can’t use SQL to calculate how much time a user spends on different pages of your UI application. You will need to implement this logic on your UI whenever there is an event such as when the user navigates to another page or a button click etc. You capture the timestamps you need on the UI and then make a database call through an SP call or Query through your server side code (such as .Net, Java or Node.js).
Once you have captured the data from the UI you will be able to implement any kind of logic on that data through an SP or a function or something like that in using SQL.
If you use TIMESTAMPDIFF(), and set its argument to SECOND, you can get back the difference of two datetime fields in a record in a manner that can be summed and divided. Documentation:
Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions.
Then use SUM() to sum up these values, and divide by the results of COUNT(). Documentation:
SUM(): Returns the sum of expr. If the return set has no rows, SUM() returns NULL.
COUNT(): Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement.
Your code will then basically look like this. You may need to make some adjustments based on your database setup.
SELECT
SUM(
TIMESTAMPDIFF(SECOND, OrigDateTime, LastDateTime)
) / (select COUNT(id) FROM yourTable)
AS average
FROM yourTable;
This, of course, follows our standard formula for calculating an average:
sum(differences) / count(differences)

SSRS 2008 R2 Subtracting current row from previous row where data is summed

I am fairly new to SSRS 2008 R2. Trying to convert my reports from Crystal. Thought this would be a simple task but this ended up being a pain. I have a table that is grouped by Year then Week_Number so the table looks this when run: (I am trying to create the total row)
Week1 Week2 Week3 Week4
2013 1 2 5 4
2014 0 3 2 6
Total -1 1 -3 2
There is a row group called Year and column group called Week_Number. I have one expression in the total column Sum(Fields!TotalProdWTD.Value).
I added a group footer to try create the total row. I am able to sum the column works fine, I can get the value of the last row by doing this ReportItems!txtTotalProd.Value. But when I try to use
=Previous(ReportItems!txtTotalProd.Value)
I get the aggregate functions can be used only on report items contained in page headers and footers message. I believe I have to add a reference to the group to make the previous function work, just not sure how to do that.
With your report table
Insert new row outside group below
And use your command:
Sum(Fields!TotalProdWTD.Value) to expression textbox
I found what I was looking for here. At the bottom of the article:
http://msdn.microsoft.com/en-us/library/ms157328.aspx

Reporting services get total sum

I want to get the total sum of a particular column in a report. I have tried using =Sum(Fields!baseline_number.Value) but this gets the sum for each row and when I use
=RunningValue(Fields!Quantity.Value, Sum, Nothing)
I get the cumulative sum. I wouldn't mind using =RunningValue(Fields!Quantity.Value, Sum, Nothing) then getting the final value because this would be the sum for all the rows. Is there a simpler way to achieve getting the total for a certain row in a report?

SUM of a SUM in SSRS 2008

I've this report
Here I make the first sum because I've grouped values from each month (months are "Gennaio", "Febbraio", "Marzo" etc etc.). These values are hidden, but anyway I get the sum and I display the sum for each month.
Then I should make the second sum that use values for each month and display the total for each category. Categories are "TOTALE LAVORI RESTAURO", "TOTALE LAVORI EDILE" etc.)
This is the final sum, where I sum values from each category.
Everything is working well, but now I have to add a "month" parameter to the report that returns sums until a selected month. This parameter changes the sum 1 with this expression:
=Sum(IIf(Fields!mese.Value <= Parameters!mese.Value, Fields!costi.Value, 0))
Now, how should I change expression in SUM2 and SUM3 to work with this parameter?
If I copy that code, ther returns #Error and as far as I know I can't use ReportItems sum.
So any suggestion?
SUM #1 could remain Sum(Fields!costi.Value) because you need to display every months.
i.e.: display GIUGNO even if Parameters!mese.Value = 4 (APRILE).
So you have only to change SUM #2 and #3 because TOTALE LAVORI RESTAURO and TOTALI must show only costi from GENNAIO to Parameters!mese.Value; i.e. if Parameters!mese.Value = 4 display only GENNAIO-APRILE even if we have details about GIUGNO.
The expression gave error because you have NULL value in Fields!costi.Value or Fields!mese.Value: convert this value to zero in your DataSet and you won't have problems.

Row total for calculated fields using SSRS Report Builder 2.0 over SQL Server 2008

I have a SRSS (2.0) tablix where I need to total the rows at group level (Contract) and DataSet Level (All Contracts)
In the detail rows I have a cell that holds a 'contract cost', a cell holding 'contract term' (months), two cells containing 'contract start date' and 'contract end date' then 12 columns for the months (Apr to Mar) of a financial year.
Each cell in a detail row has an expression that calculates:
<if this month is in the contract period then 'contract cost / contract period = result>
The results in coloumns are totalled so thet each month has a total expenditure by individual contract and all contracts
But I can't find a way to total the rows. Contracts may start or end at any period through the Financial Year so I need to row-total each detail line.
On the SQL Server 2008 I don't have authority to create tables/views, temp or perm. Otherwise I'd do that.
Any ideas?
And I'd put up an example but can't seem to get an image uploaded and accepted.
Thanks for looking...even more gratitude for resolving.
Brian
you could create a a few variables and have them return the running totals
Public Shared TotalProdActual as double
Public Function AddTotalProdActual(ByVal balance AS double ) AS double
TotalProdActual = TotalProdActual + balance
return balance
End Function
Public Function GetTotalProdActual()
return TotalProdActual
End Function