Microsoft Access - Group By Condition - ms-access

I have a column in one of my tables called revenue.
The revenue values can be positive or negative.
Say there are revenue values such as +100 and -100.
Is there a way I can group by revenue, such that it does not read the sign? Meaning it reads -100 and +100 the same?

Related

How to get the right result for average from averages in ssrs?

I have report with totals.
In the end I have average values from route points.
I want to get the average value per route points.
results
for example average for 2041 is 00:00:12
average for 2042 is 00:00:04
I want to get average from from 2041 & 2042
I received 00:00:12 which is not true..
For average and avg of avg i used the same expression :
=Format(
TimeSerial(0,0,
Round(
IIf(sum(Fields!N_ANSWERED.Value)=0,
0,
sum(Fields!T_ANSWERED.Value) / iif(sum(Fields!N_ANSWERED.Value)=0,1,sum(Fields!N_ANSWERED.Value))
)
)
),
"HH:mm:ss")
I expected ~ 00:00:08 as result.
An average of averages is rarely right.
For example, in group 2041 you appear to have higher call volume at lunch time, (12:00 to 13:00) as the calls take longer to answer, and lower call volume first thing in the morning (8:00). Let's say the average time to answer at 13:00 was 00:00:24 because 50 calls came in, but at 8:00 there was only one call which took 00:00:02 to answer. Now, the average of those two hours isn't (00:00:24 + 00:00:02) / 2 = 00:00:13 because the amount of calls is very different in the two samples making up the average.
The real average is the average for that group multiplied by the number of calls in that group divided by the total calls (00:00:24 x (50/51)) + (00:00:02 x (1/51)) = 00:00:23.57
If you are rounding to a precision of zero decimal places, that is still 00:00:24.
This is called the weighted average as each group's average influences the outcome depending on how many results are in the original calculation of the average for that group.
This is why your averages of 00:00:12 and 00:00:04 probably won't be 00:00:08, it will vary depending on how many calls are in each group. Now, if there are exactly the same number of calls in each group, then the average of averages will be the same as the weighted average (this is the only case where you would get 00:00:08).
The closer the total number of calls are in each group, the closer to the right result the average of averages will be, but it is an unreliable calculation. Conversely, the more the number of results in each group varies, the more the weighted average will skew towards the average of the group that is more highly represented in the results.
Now, if there are a lot of results in the 2041 group and very few in the 2042 group then the 00:00:04 average result for 2042 will hardly influence the overall average, which may lead to the outcome where the result for 2041 overwhelms the result for 2042 and the overall average is the same as the average for 2041 within your level of precision and rounding, as per the example above.
The fact that there are several missing hours in the 2042 result set makes me think this is the case.
So your calculation looks correct - the sum of the time taken to call divided by the number of calls will give you the average for the groups and for the overall average. It is just that the average of averages won't be the same result because the groups aren't equally represented in the data used to calculate the overall average.
Based on your expression, your overall average looks accurate at 00:00:12.
By referencing the rendered cell rather than the dataset filed you can so this quite simply.
1. Get the name of the cell containing the detailed average you have already calculated, let's assume this is called textbox1.
Then your expression is simply
=AVG(ReportItems!textbox1.Value)

How to get weighted average for CTR in MySQL?

Here is what my data looks like:
I would like to see a roll up of the total impressions, total clicks and average ctr for a given query. E.g.,
Except this is just an average of the ctr column values; whereas I need a weighted average.
I think this SO answer Calculate Weighted Average in sql for duplicate items is taking me most of the way there, but since this calculation involves two metrics (ie, impressions and clicks), it's a bit different.

Select average value X of SQL table column while not grouping by X

For the purposes of my question, I have a database in a MySQL server with info on many taxi rides (it is comprised of two tables, history_trips and trip_info).
In history_trips, each row's useful data is comprised of a unique alphanumeric ID, ride_id, the name of the rider, rider, and the time the ride ended, finishTime as a Y-m-d string.
In trip_info, each row's useful data similarly contains ride_id and rider, but also contains an integer, value (calculated in the back end from other data).
What I need to do is create a query that can find the average of all the maximum 'values' from all riders in a given time period. The riders included in this average are only considered if they completed less than X (let's say 3) rides within the aforementioned time period.
So far, I have a query that creates a grouped table containing the name of the rider, the finishTime of their highest 'value' ride, the value of said ride, and the number of rides, num_rides, they have taken in that time period. The AVG(b.value) column, however, gives me the same values as b.value, which is unexpected. I would like to find some way to return the average of the b.value column.
SELECT a.rider, a.finishTime, b.value, AVG(b.value), COUNT(a.rider) as num_rides
FROM history_trips as a, trip_info as b
WHERE a.finishTime > 'arbitrary_start_date_str' and a.ride_id = b.ride_id
and b.value = (SELECT MAX(value)
from trip_info where rider = b.rider and ride_id = b.ride_id)
GROUP BY a.rider
HAVING COUNT(a.rider) < 3
I am a novice in SQL but have read on some other forums that when using the AVG function on a value you must also GROUP BY that value. I was wondering if there is a way around that or if I am thinking of this problem incorrectly. Thanks in advance for any advice / solutions you might have!
The following worked for me:
SELECT AVG(ridergroups.maxvalues) avgmaxvalues FROM
(SELECT MAX(trip_info.value) maxvalues FROM trip_info
INNER JOIN history_trips
ON trip_info.rideid = history_trips.ride_id
WHERE history_trips.finishTime > '2010-06-20'
GROUP BY trip_info.rider
HAVING COUNT(trip_info.rider) < 3) ridergroups;
The subquery groups the maximum values by rider after filtering by date and rider count. The containing query calculates the average of the maximum values.

Calculated Query Field ignoring some results

I have a calculated field in a query that takes our total revenue per customer and subtracts out total costs per customer to get our margins with that customer. The formula works fine as long as the customer has a cost value but if the customer has no cost, the formula is ignoring that revenues exists and is excluding that customer from the total and the query does not include them in the returned result. Is there a way to fix the formula to include these customers with no costs but still generate revenue?
SELECT [Company Information].[Company Name], ([2 YTD - Customer
Revenues].SumOfSumOfCharge-[2 YTD - Customer Costs].[SumOfTotal Cost])
AS [Customer Margin]
For a query run from within an Access session, you can use Nz to have Access substitute 0 for Null when it computes the difference ...
Nz([2 YTD - Customer Costs].[SumOfTotal Cost], 0)
Alternatively you could use an IIf expression to accomplish the same thing ...
IIf([2 YTD - Customer Costs].[SumOfTotal Cost] Is Null, 0, [2 YTD - Customer Costs].[SumOfTotal Cost])
Support for IIf is built into the db engine, so it is available for any Access query. Nz is an external (VBA) function which the db engine can use within an Access session. So the IIf approach should theoretically be faster, but may not be noticeably faster in practice.

Aggregate function SUM() for money

Requirement is add salary of employee i m using SUM() , salary is in this format 1,00,005.00 so when 1,00,005.00 + 3,00,005.00 it gives result as 4 not actual result ,
this is query ---->
SELECT employee.name, SUM( department.salary ) AS Salary
FROM department
LEFT JOIN employee ON department.employee_id = employee.id
GROUP BY name
Try to remove all "," in salary and then use sum(). you can use mysql replace function
SELECT employee.name, SUM( replace(department.salary,",","") ) AS
Salary FROM department LEFT JOIN employee ON department.employee_id
= employee.id GROUP BY name
The main question is whether your currency is a decimal type or not. Your format "1,00,005.00" with groups of two and three digit makes me wonder.
If it is, if "1,99,997" + "0,00,003" is equal to "2,00,000", then you can use Hắc Huyền Minh's answer and convert it to a decimal equivalent straight away.
Before doing that, though, I would run an acceptance test on the database to verify that all values are indeed in the form you expect. Otherwise you might introduce a bug in the workings. VARCHAR type has no validation whatsoever, and someone could have a salary of "1,0X,000.15" which would be considered equivalent to ten after the conversion.
If your currency is not a decimal type - for example if "1,05,03" means "one pound five shilling three pennies" - then "1,05,03" + "1,19,09" is not "2,24,12" but rather "3,05,00" - since twelve pennies, not ten, make a shilling, and it takes twenty shillings to make a pound.
In this case you either convert it to decimal via weighting, or you select all the rows and do the sum in code instead of in MySQL. It would be possible also to implement a UDF to perform either the sum or the conversion to decimal.
For weighting, you consider that if one pound is made up of 20 shillings, then each shilling is 1/20 of one pound, and so equals 0.05; so "1,05" becomes 1 + 5*0.05 = 1.25. For pennies, twelve pennies make one shilling which is 0.05 pounds, so one penny is 0.05/12 = 0.00416666... (and there you see a difficulty. You will need a larger numeric type to prevent numeric errors from creeping in).