Display grand total as the sum of averages (custom subtotal) - asp.net

I have SQL Server query, which returns the following data:
I want to display the data in RDLC 2008 Report using matrix format which should give the following result:
The Grand Total of Qty field should return 12 for January & 14 for February.
I have tried many different methods one of which is by using the following expression in the matrix 'Qty' textbox :
=IIF(InScope("RowGroup_Category")
,IIF(InScope("RowGroup_SubCategory")
,Fields!Qty.Value
,Code.GetAverageMemberCount(Cint(Avg(Fields!Qty.Value)))
)
,Code.TotalMemberCount
)
The above functions are written in Report Properties Code as below:
Public Dim TotalMemberCount As Integer = 0
Function GetAverageMemberCount(ByVal AverageMemberCount As Integer) As Integer
TotalMemberCount = TotalMemberCount + AverageMemberCount
Return AverageMemberCount
End Function
I have also tried RunningValue(Fields!Qty.Value,Sum,"RowGroup_Category") and many such functions but I am unable to get the exact results. any help would be appreciated.. Thank you

Try adding this into a new column as a test:
RunningValue(Avg(Fields!MemberCount.Value,"RowGroup_Category"),SUM,Nothing)
If the value is correct you should be able to change SUM into MAX when setting this expression in the grand total field.
You can refer to the total like code.TotalMemberCount instead of using a get function
but i don't think you need this function in this case.
Check the following blog for a simular variable referencing situation

The only solution I could find that worked for me to solve this is to calculate the averages in a different dataset and use lookup functions to fill the grand total from there.
In your case I would add a key column in your original dataset:
select Category + '|' + Month as key, Category, SubCategory, Month, Qty, Amt
from YourTable
Create another dataset using:
select Category + '|' + Month as key, Category, Month, avg(Qty)
from YourTable
group by Category, Month
Add the second result as DataSet2 to the report. (In Visual Studio in the Report Data pane right click on DataSets.)
Add to the Report Properties -> Code section the following:
Function SumArray(varArray as array) as Decimal
Dim retVal As Decimal = 0
For Each item As Decimal In varArray
retVal = retVal + item
Next
Return retVal
End Function
Finally in the report use the following expression for Grand Total under Qty:
=code.SumArray(Lookupset(Fields!key.Value, Fields!key.Value, Fields!qty.Value, "DataSet2"))
P.S.: Make sure that the second dataset is also filled by your code the same way the original was.

Related

SQLite3 creating a view profit combining rows twice based on column

Given a table
symbol|action|quantity|price
foo|buy|1|-10.00
foo|sell|1|10.50
bar|buy|1|-50.00
bar|sell|1|50.50
I am trying to create a view that shows profit per symbol
I've tried several variations of below with multiple 'order by' but not getting the expected output
create view if not exists profit_view as select symbol,sum(price * quanity) as profit from trans group by symbol;
What I'd like is a view that shows or something close to that
symbol|profit|percent
foo|.50|5
Assuming each symbol only has a single buy and sell record we can try:
SELECT
symbol,
SUM(quantity*price) AS profit,
100.0 * SUM(quantity*price) /
ABS(MAX(CASE WHEN action = 'buy' THEN quantity*price END)) AS percent
FROM trans
GROUP BY
symbol;

Logic statement within a loop for ASP Classic

What would be the logic statement to notify the user that they purchased the same product within the last 90 days?
SQL = "SELECT id, PurchaseDate, ProductName from TableName where id = " & id)
Do Until objRS.EOF
if (objRS("ProductName") and objRS("PurchaseDate") <= dateadd(day,-90,getdate())) and objRS("ProductName") and isNull(objRS("PurchaseDate")) then
AlreadyPurchased = true
end if
objRS.movenext()
Loop
My loop that writes it to the page:
<%=objrs("ProductName")%><% if AlreadyPurchased then%>Last Purchased with last 90 days<%end if %>
The best answer depends on a few assumptions:
A) your query does not return any NULL values, and
B) that you have previously stored the product name in a preexisting variable.
Your code has some issues.
1) if (objRS("ProductName") is basically asking if the fieldset value is a True boolean value. My first assumption is that you already know the product name that you're testing. So, the above snippet should be replaced with this: if (objRS("ProductName").value = strMyProductName where strMyProductName is a variable that stores the product name (as a string).
2) You should consider storing the calculated date in a variable outside the loop. No need to repeatedly compute the date if you can compute it once, store it in a variable, and use the variable in your comparison.
3) Remove the last component of your conditional and objRS("ProductName") because it's redundant and has the same faulty logic as what I explained in (1) above.
4) Your DateAdd() can be written better.
TL;DR
dim strMyProductName, dat90, AlreadyPurchased
AlreadyPurchased = false
strMyProductName = "Shoes"
dat90 = dateadd("d", -90, now) '90 days ago
Do Until objRS.EOF
if cstr(objRS("ProductName").value)=strMyProductName and cdate(objRS("PurchaseDate")) <= dat90 then
AlreadyPurchased = true
exit do
end if
objRS.movenext()
Loop

MS Access calling a function

I am writing a programme for membership of a club in Access. Cell 1 will contain a number and cell 2 will contain cell 1 number in roman numerals.
I have a function that will convert the number but am having trouble getting cell 1 value into the function and the answer into cell 2. The start of the function is
Public Function RomanNumeral(ByVal aValue As Long) As String
and ends with
RomanNumeral = strResult
I would be very happy if anyone can help
Ok, this would be a calculated field. For this, you use =myFunction() as ControlSource.
In your case, if the number field has the name myNumber, use this for the roman number field:
=RomanNumeral([myNumber])
Edit
If you don't want a calculated field, but a field in the table, create an AfterUpdate event procedure for the number field, where you set the second field:
Private Sub myNumber_AfterUpdate()
' Use Nz to avoid runtime error when myNumber is NULL
Me!RomanNumber = RomanNumeral(Nz(Me!myNumber, 0))
End Sub

Loop for each day in the current month and check rows for a match

How can I write a loop to run for each day in the current month and then check to see if the Dataset has a record that matches one of those days and take an action?
For Day As Integer = 1 To DaysInTheMonth
For Each row In MyRows
If row.Date.Day = Day Then
' Yup! Found Data for this day!
' Add Data to string
Else
' No Data To Display
' Add Blank Field String
End If
Next
Next
This generates too many rows in the table I have it building. Basically, every day gets as many rows as there are that contain data.
I'm building a HTML table that gets mapped into a chart using jquery so I need a for everyday of the month.
I think what would more suit your run would be having a day counter in the loop for the rows, so that you don't actually run over all the days again for each record in the datatable.
So now if you have the record its added to the string else skipped and the dat is incremented, so going down further you should get the required datapoints. give it a try
For Each row In MyRows
If row.Date.Day = Day Then
' Yup! Found Data for this day!
' Add Data to string
Else
' No Data To Display
' Add Blank Field String
End If
Day = Day + 1;
Next
You should drop the first For loop as that's causing all rows to be selected. Instead set Day = Today. Ie
Day as Integer = date.day

please help me in creating a sort expressions for datatable in vb.net

I have got a datatable which contains many columns in which three are main:
hotelid
dshotelid
hotelname
Some hotels contain only dshotelid, some contains only hotelid and some contain both dshotelid and hotelid.
I need sort in such way so that those hotels who got both dshotelid and hotelid should be on top and then those hotels who have got only dshotelid and at last those hotels who have got only hotelid...
Please help me to create such a sort expression.
I created this :
dtmaintable.DefaultView.Sort = "dshotelid, hotelid"
but it Is not giving me desired output.
Fields with NULL in them tend to appear at the top of sorted columns (unless the sort is in descending order) so you're probably seeing rows with both hotelid and dshotelid at the bottom, instead of the top?
How is this data table populated? If it's coming from a database query, it's a simple matter to construct an additional column (or columns) which contain(s) whatever sort key you need - be it an amalgum of other columns or some other unique identifier.
EDIT: Feb 4, 2010 - in response to your 'FilterAndSortTable' solution:
Your solution works but it's not because of the FilterAndSortTable - it's because you used a different sort order.
Originally, you used "dsohtelid, hotelid".
The second time, you used "dshotelid desc, hotelid desc".
This has the effect of putting your non-nulls at the top and nulls at the bottom, but I would dispute that this qualifies as a good solution.
Your id's are now sorted backwards - which I kind of assumed you might want to avoid, hence my suggestion of a new sort column that would respect this.
Still, if the order of id's doesn't matter then your solution is fine and you can simply stick with your original code, with the addition of 'desc', like so:
dtmaintable.DefaultView.Sort = "dshotelid desc, hotelid desc"
Of course, if anything I've said here has been of any use to you, a tick would be muchly appreciated. It would help me reach 50 rep points and finally be able to write comments. :)
i got the answer its :
datagrid1.datasource = FilterandSortTable(dtmaintable,"", "dshotelid desc, hotelid desc")
Public Shared Function FilterandSortTable(ByVal SourceTable As DataTable, ByVal strFilters As String, Optional ByVal strOrder As String = "") As DataTable
Dim Tbl As DataTable = SourceTable.Clone
Dim rows() As DataRow = SourceTable.Select(strFilters, strOrder)
For i As Integer = 0 To rows.Length - 1
Tbl.ImportRow(rows(i))
Next
Return Tbl
End Function

Resources