Choosing which rows to sum and average in either SSRS or SQL - sql

ROW column 1 column 2
1 A 1
2 A 1
3 A 3
4 A 1
5 A 2
6 B 1
7 B 3
8 B 1
Pic of table
Lets say I have this table as shown above. I want to be able to average SELECTED values from column 2. Am I able to use any function in SSRS that allows me to select which value to use to average? The end goal is to allow the user to interactively choose which value to average.
For example if I would want to use ("Row 1 + Row 2 + Row 4")/3, or (Row 6 + Row 8)/2, how can I go about letting the end user to choose those values to average?
Is there something that I need to do in SQL first to make it easier in SSRS?

The idea is by using report parameter and dataset filter
Add parameter in SSRS to allow user input of multiple values, set the available values for row-1, row-2, and so on
here for your reference how to add the parameter in SSRS
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/add-change-or-delete-a-report-parameter-report-builder-and-ssrs?view=sql-server-ver15#:~:text=To%20add%20or%20edit%20a,or%20accept%20the%20default%20name.
after you add the parameter, let's say you already have a dataset which is SQL query such as:
SELECT *
FROM the_table
Right click on your dataset, on properties, in the filter tab, add a filter for the column ROW IN parameter that you have made earlier
after you add filter on your dataset, on your report, simply use that dataset and put expression AVG(Column 2)

Related

Access filters on splitform through query to report

I have a splitform and what I want to do is open report on filter I use in this spltiform. What's important is that in this splitform you can do the correction of the row (you can't edit the previous row, but it creates two additional rows - one is negative and the other is to edit and I have one column which stores the ID of corrected row - CorrectedRowID). So I can't create report based on that table, so I created a query which groups this data by CorrectedRowID and it takes the last row of each ID so I have only the newest row of every ID. Then based on that query I created report.
On the splitform I have button which opens the report. But what I want is that when I have filter in this splitform and I click on the button Open Form it opens the report already on the filters. I have used this code in this button in VBA:
Private Sub Command282_Click()
DoCmd.OpenReport "tb_ewid_WNT1_raport", acViewPreview, , Me.filter
End Sub
When I create a normal report (not based on query) filter works great, but when I put some filter on the data in this splitform on one column it asks me for the value of the column on which I want to filter my data.
The query returns the newest rows of each ID. For example:
ID
Name
Quantity
Price
CorrectedRowID
IsCorrection
1
bread
4
1,5
1
No
2
milk
3
2,3
2
No
3
bread
-4
1,5
1
Yes
4
bread
11
1,5
1
Yes
In this case query will return this, so for each CorrectedRowID it returns the highest ID:
ID
Name
Quantity
Price
CorrectedRowID
IsCorrection
2
milk
3
2,3
2
No
4
bread
11
1,5
1
Yes
I created the report based on this query. And I also created a button on splitform which uses the code I wrote up. When I filter my data and when I click on this button it asks me for the value for some column (it's not the same every time). The values that it asks for are combo boxes which take the values from not the same table. The query uses JOIN.
Where did I make mistake?

Updating columns based on a combine rows value on the same table

Please assist if possible, I have used Stuff to combine rows into a single row based on other columns. However I want to turn each of the unique items into it's own column with a number showing if it exists, e.g. 1 or 0 and then doing the same for all subsequent rows?
I have been able to create the columns but I can't get them to update per whats in the one column.
But I want it to be dynamic so matter how many different names appear in categories it creates a new column and adds 1 or 0 if it appears or not
How about something like this for SQL Server?
strSQL = "SELECT Category, CASE WHEN Category IS NOT NULL THEN 1 ELSE 0 END AS IsCategoryExist FROM MyTable"
Sample data (the 2nd column shows as 1 if the first column is non-blank):
Cars, 1
[Blank], 0
Airplanes, 1
Radios, 1

How to combine a row of cells in VBA if certain column values are the same

I have a database where all of the input from the user (through a userform) gets stored. In the database, each column is a different category for the type of data (ex. date, shift, quantity, etc) and the data from the userform input gets put into its corresponding category. For some of the data, all the data is the same except for the quantity. I was wondering how I could combine these rows into one and add the quantities to each other for the whole database (ex. combining the first and third data entries). I have tried playing around with a couple different loops but can't seem to figure anything out.
Period Date Line Shift Type Quantity
4 x 2 4/3/18 A 3 14 18
4 x 2 4/3/18 A 3 13 12
4 x 2 4/3/18 A 3 14 15
Thank you!
If you're looking to modify the underlying database, you might be able to query the data into the format you want by including all the other columns in a GROUP BY statement, save the result to another table, then replace the original table with the properly formatted one.
If you have the data in Excel and you just want to view it with the duplicate rows summed, a Pivot Table would be a good choice. You can select all the other columns as rows for the Pivot Table and sum of Quantity as the values.

subtract every next column value from previous?

I have a dataset, where somehow the next singular data is added on top of the previous data for one row, and that for every column, which means,
row with ID 1 is the original pure data, but row with e.g ID 10 has added the data from the previous 9 datasets on itself...
what I now want is to get the original pure data for every distinct item, which means for every ID, how can I substract all data from lets say ID, 10? I would have to substract those of the previous one, for ID 9 and so on...
I want to do this either in SQL Server or in Rapidminer, I am working with those tools, any idea?
here is a sample:
ID col1 col2 col3
1 12 2 3
2 15 5 5
3 20 8 8
so the real correct data for Item with ID 3 is not 20, 8, 8 it is (20-15),(8-5),(8-5) so its 5,3,3...
subtract the later from its previous for every item except the first..
1 12 2 3
Try it out with lag series operator, it will work for sure! To get this operator you should install the series extension from the RM marketplace.
What this operator does - he copies the selected attributes and pushes every row of the example set for one point, so row with ID 1 gets a copy with ID 2 etc (you can also specify the value for a lag). Afterwards you can substract one value from another with Generate Attributes.
I think lag() is the answer to your question:
select (case when id = 1 then col
else col - lag(col) over (order by id)
end)
However, sample data would clarify the question.
Within RapidMiner there is the Differentiate operator contained in the Series extension (which is not installed by default and needs to be downloaded from the RapidMiner Marketplace). This can be used to calculate differences between attributes in adjacent examples.

Sum of a record source in access forms

I have a query that has the following display:
type $ value
1 5
1 3
1 2
2 1
2 3
This query is the record source of my form I would like the form to display the data as so
type Sum of value
1 10
2 4
is there a vba or setting way to achieve that ?
The proper way to do that is to use an aggregation function in your query, in your case it would be sum. Your query will look like this.
SELECT Type, SUM(value) As Total FROM table GROUP BY Type;
if you use the graphical query builder then you will have to follow this