SQL: How can I find the total and average value in one SQL statement? - sql

I would like to calculate the total amount of expenditure and the calculate the average value from the number of rows like this:
SELECT AVG(SUM(expenditure)) from INCOME;
However, there is a error say "misuse of aggregate function sum()"
how can I achieve this?

You can't calculate the average of the total sum as there is only one total.
The average AVG() function already calculates the total as part of its logic.
This is what you want:
SELECT AVG(expenditure) as AverageExpenditure,
SUM(expenditure) as TotalExpenditure
from INCOME;

SELECT AVG(expenditure) AS avg_exp, SUM(expenditure) AS sum_exp FROM INCOME;

Related

How to get the weighted average in sql?

I am trying to calculate the weighted average and have used the below query.
select di.name, count(di.name)
,dmic.c_name,
sum(count(di.name)*dmic.c_price)/count(di.name) as avgPrice
from di
join..
on..
join..
on..
where...
group by 1,3
order by 2 desc;
And I am getting the error: aggregate function calls may not have nested aggregate or window function.
How can I work around this? I am looking to get the name, its occurrences and its weighted price in the output.

Total Average, ignoring GROUP BY

I created a query in Microsoft Access such as the one below:
SELECT LoanType
,Avg(Loan Amount)
,Avg(Loan Rate)
FROM Table1
GROUP BY LoanType
The output is as you would expect, the average loan amount and the average loan rate for each loan type.
However, I'd like for my Access Report to calculate the average of all the loans, regardless of loan type, and place this row at the very bottom. Using the Report view in Access, you can add a "Totals" row where you can write a formula such as COUNT(), SUM(), AVG(). But as you know, calculating an average of an average goes against basic math.
I'm assuming I have to create this "Totals" row at the SQL/Query level. But I can't seem to figure it out. Any input would be greatly appreciated!
You can use UNION ALL to add a row with a similar query, just without the GROUP BY and a NULL for LoanType (or any other value you like as long as it's implicitly castable to the data type of LoanType).
SELECT LoanType,
Avg(Loan Amount)
Avg(Loan Rate)
FROM Table1
GROUP BY LoanType
UNION ALL
SELECT NULL,
Avg(Loan Amount)
Avg(Loan Rate)
FROM Table1;
Of yourse you can do exactly what you described: Build a query that calculates the averages by LoanType, build a report on this query and calculate an average in the report footer. Unfortunately, this "total average" will be an average of averages, but I guess you want an average over all records. To achieve this:
Base your report on Table1.
Create a group to group by LoanType.
Calculate the averages by LoanType in the group footer.
If you don't want to see the details, set the details section to be invisible.
Calculate the "total averages" in the report footer.

Getting average low and high bids in SQL

I am pretty new to SQL and am working with a (what I expected to be easy) little bidding tool.
I am trying to compute average lows and highs from the same column. I have managed to figure out how to use SQL's MIN, MAX, AVG functions, but how would I go about averaging MIN and MAX?
This is the query I am using:
$query = $pdo->prepare("SELECT AVG(bid),MIN(bid),MAX(bid) FROM bidding WHERE bid_id=:bid_id GROUP BY bid_id");
Try the following query to accomplish task
SELECT ((max(bid)+min(bid))/2) as average FROM bidding WHERE bid_id=:bid_id GROUP BY bid_id
Because the predefined avg function takes only one argument that may be column from table or single value. So you have to find the average of the min and max value of bid like above
As You are saying you need to find out the Min of Avg and Max of Avg,
Now what you are doing is group on one column this means Avg(Bid) will return only one value. And the thing that you are doing will make sense only if it is done with two column,
For example you wanna know the min of Averages per day. You need to identify one more column on which base you want to find out max and min of Avg. See in my example i am using Date as second column. the query will go like.
Select Max(MAx_Bid),Min(Min_Bid),Min(Avg_Bid),Max(Avg_Bid) FROM
(SELECT AVG(bid) Avg_Bid,MIN(bid) Mix_Bid,MAX(bid) max_bid FROM bidding WHERE bid_id=:bid_id GROUP BY bid_id,Days_Date(Dummy column))A

How do I Sum the values of a Count Distinct Aggregate of Contained Groups

I've got a bit of a problem with a grouping. At the moment I have a grouping that does a CountDistinct(Fields!AccountName.Value). This grouping is on a Financial Time Period.
I need to be able to do a Sum on the values brought forward by this CountDistinct at the end of this report, but I can't put an aggregate function within an aggregate function.
Don't suppose you guys have any idea's / help?
Thanks.
You can do a select and aggregate your set of aggregates. Something like:
SELECT sum(total)
FROM (Select count(Fields!AccountName.Value) as total, name FROM x group by name) totals

calculate the average cost

How do I get the average cost per visit in a TSQL statement?
I have the total count of visits and summed the cost.
You can use the AVG aggregate function:
http://msdn.microsoft.com/en-us/library/ms177677.aspx
Can't you simply divide the number of visits by the total cost? (I.e., SUM(Cost) / SUM(Count))
If you have the total count and summed cost, the average is just a division away:
summed cost
------------- = average cost
total count