Average sum in Yii2 - yii2

I have an object of massives -
How to count an average sum of all rating properties

$averageRating = UserRating::find()->where(['user_id'=>3])->average('rating');
ref - https://www.yiiframework.com/doc/api/2.0/yii-db-query#average()-detail

Related

MySQL Sum of Values In a Single Column For Every 50 Rows

I have a very simple query like this for my event_prizes table:
SELECT id, prize FROM event_prizes WHERE event_prizes.event_id = x;
Instead of getting the individual price amounts, I need to show the total amount of the given prize for every 50 rows in this query. How can I use the SUM function to calculate the total value of every 50 rows?
assuming you have a colum with the row number (this id db depending )
You could try grouping by the floor(your_row_num/50)
SELECT floor(your_row_num/50), sum(prize )
FROM event_prizes WHERE event_prizes.event_id = x
GROUP BY floor(your_row_num/50);
if you have a mysql version 8 you could use ROW_NUMBER otherwise
use a var and increment

SSRS - Percentage Totals Row

I am trying to build an SSRS report that has a column based on a percentage:
AgentSales AgentCommission Commission
20 .25 5
33 .12 3.96
46 .76 34.96
Totals: 99 [Unknown Calculation]
However, where I am having problems is I need to get what would be the totals column? How would I calculate the Commission so that the number shown in [Unknown Calculation] to display what I assume would be the average commission?
Commission column is simply AgentSales * AgentCommission. The total commission wouldn't be a SUM(Commission), but would more or less be the average commission for all agents.
Assuming you want to calculate the weighted average commission then I would calculate it like this..
Expression 1: is simple as per your sample.
=Fields!AgentSales.Value * Fields!AgentCommission.Value
Expression 2: is the sum of the results of expression 1
=SUM(Fields!AgentSales.Value * Fields!AgentCommission.Value)
Expression 3: is the expression 2 divided by the sum of agent sales
=(SUM(Fields!AgentSales.Value * Fields!AgentCommission.Value))
/
SUM(Fields!AgentSales.Value)
The other cells should be self explanatory...
When we run the report we get the following.
If this is not what you wanted, then please edit your question to show the desired result and how you arrived at that result.
EDIT AFTER UPDATE FROM OP
If you want the cell marked "2" in my sample to be an average of the values above then simpley change it to
=AVG(Fields!AgentSales.Value * Fields!AgentCommission.Value)

SSRS 2012 MATRIX TOTAL DYNAMIC COLUMNS BY COLUMN HEADING

Simple Q:
In SSRS 2012: require a matrix with column totals derived from the dynamic column headings;
e.g.
Period 1 2 3 Total Total
Type Act Bud Act Bud Act Bud Act Bud
Total 10 9 10 9 10 9 30 27
is this possible in SSRS?
Can easily get the total of the sum of columns -(57) but not split by type.
Thank You.
data is grouped ;
Business Unit - Row Group
Account Type - Row Group
Month Period - Column Group
Amount_Type (Act, Bud) - Column Group
The Aggregate is Amount
Tks
Without knowing the structure of your dataset I think you can add two columns outside the columns groups at the right side and use these expression to calculate the respective total:
For Actual total:
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0))
For Budget total:
=SUM(IIF(Fields!Type.Value = "Bud",Fields!Amount.Value,0))
UPDATE: Try setting the scope of the SUM function.
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0),"DataSetName")
Replace DataSetName by the actual name of your dataset.
Let me know if this helps.

How to substract group by total in SSRS?

In this Image I have three groups, the grand child group is pp
and this will always have two values 1 and 2 between these group there will be multiple rows for pp=1 and pp=2 now i have to substract pp=2 QTY total from pp=1 Total Please help me. on the last of group as grand total.
Use this expression:
=SUM(IIF(Fields!pp.Value = 1,Fields!IssueQty.Value,0) -
SUM(IIF(Fields!pp.Value = 2,Fields!IssueQty.Value,0)
The expression must be in the scope of the details or pp group.
Let me know if this helps.

GroupBy and get percentage for each

I have my SQL table like this:
**CLIENTS:**
id
country
I want to echo a table with all countries I have with percentage fo each.
For example, if I have 2 Canadians and 1 French in my table, I want:
1 - Canada - 66%
2 - France - 33%
What I tried:
SELECT country FROM `mytable` GROUP BY `Country`;
It works, but how to have the percentage for each ?
Thanks.
You can use subquery:
SELECT
country,
COUNT(id) * 100 / (SELECT COUNT(id) FROM `mytable`) AS `something`
FROM
`mytable`
GROUP BY
`Country`;
You don't specify a falvor of SQL, but years ago microsoft posted their suggested solution:
select au_id
,(convert(numeric(5,2),count(title_id))
/(Select convert(numeric(5,2),count(title_id)) from titleauthor)) * 100
AS "Percentage Of Total Titles"
from titleauthor group by au_id
To calculate the percentage of total records contained within a group
is a simple result that you can compute. Divide the number of records
aggregated in the group by the total number of records in the table,
and then multiply the result by 100. This is exactly what the
preceding query does. These points explain the query in greater
detail:
The inner nested query returns the total number of records in the
TitleAuthor table: [ Select convert(numeric(5,2),count(title_id)) from
titleauthor ]
The value returned by the COUNT(title_id) in the outer
GROUP BY query returns the number of titles written by a specific
author.
The value returned in step 2 is divided by the value returned
in step 1, and the result is multiplied by 100 to compute and display
the percentage of the total number of titles written by each author.
The nested SELECT is executed once for each row returned by the outer
GROUP BY query
The CONVERT function is used to cast the values
returned by the COUNT aggregate function to the numeric data type with
a precision of 5 and a scale of 3 to achieve the required level of
precision.