sql average of a sum divided by a count - sql

I am trying to do an SQL query to get the average spend for a specific customer.
I have written the following SQL (this is slighlty cut down for this example)..
SELECT SUM(price) as sumPrice, COUNT(transactionId) as transactionCount, customerName
FROM customers, transactions
WHERE customers.customerId = transactions.customerId
AND transactiontypeId = 1
GROUP BY customers.customerId
This gives me the sum of the transaction and the count. With this I can then divide the sum by the count to get the average spend. However I would like to be able to get the Average as a value straight out of the database rather than manipulate the data once I have got it out.
Is there any way to do this? I have played around with writing a select within a select but haven;t had much luck as of yet, hence asking on here.
Thanks in advance

MySQL has a mean average function built-in.
SELECT AVG(price) AS averageSpend, customerName
FROM customers, transactions
WHERE customers.customerId = transactions.customerId
AND transactiontypeId = 1
GROUP BY customers.customerId

Related

SQL GROUP BY vs AVG (Problem 25 of SQL Practice Problems Book)

The question is taken from problem 25 of SQL Practice Problems Book...
[Problem 25][1]
[1]: https://i.stack.imgur.com/fmJjs.jpg
High freight charges
Some of the countries we ship to have very high freight charges.
We'd like to investigate some more shipping options for our customers, to be able to offer them lower freight charges.
Return the three ship countries with the highest average freight overall, in descending order by average freight.
My first intuition was to run the following query:
SELECT shipcountry, freight
from orders
GROUP BY shipcountry
ORDER BY freight DESC
LIMIT 3;
which is apparently not the correct way to do it. The query below uses avg aggregation function, and is the correct way to query. I wanted to know why we need to run AVG function when we have the GROUP BY?
SELECT shipcountry, avg(freight) as mean_freight
FROM orders
GROUP BY shipcountry
ORDER BY mean_freight DESC
LIMIT 3;
The question asks about average freight cost per country; that strongly implies you'll need to use the AVG function (unless you like making extra work for yourself and you decide that using SUM and COUNT is more interesting in some way).
You can't avoid using aggregates somewhere along the line since the query is about aggregate values.

PostgreSQL question - finding sum of sales by state

I need help writing the query, we have a table called SALES, which has 3 columns as below:
Column Names: sale_id, state, sale_amount_cents
I assume the sale_amount_cents has the sale amount in cents as opposed to dollars, and our end answer needs to be in dollars so we would have to multiply by 100.
Can someone please help writing the query to sum sales, in dollars, by date, rounding to two decimal places, and sorting from the greatest sale amount to the least?
I assume the query would look like this:
UPDATE SALES SET sale_amount_cents=sale_amount_cents*100
SELECT SUM(sale_amount_cents) from SALES
GROUP BY STATE
ORDER BY sale_amount_cents DESC;
select state, SUM(sale_amount_cents)/100 as Sales_in_dollar from SALES
GROUP BY STATE ORDER BY SUM(sale_amount_cents) DESC

Optimize Average of Averages SQL Query

I have a table where each row is a vendor with a sale made on some date.
I'm trying to compute average daily sales per vendor for the year 2019, and get a single number. Which I think means I want to compute an average of averages.
This is the query I'm considering, but it takes a very long time on this large table. Is there a smarter way to compute this average without this much nesting? I have a feeling I'm scanning rows more times than I need to.
-- Average of all vendor's average daily sale counts
SELECT AVG(vendor_avgs.avg_daily_sales) avg_of_avgs
FROM (
-- Get average number of daily sales for each vendor
SELECT vendor_daily_totals.memberdeviceid, AVG(vendor_daily_totals.cnt)
avg_daily_sales
FROM (
-- Get total number of sales for each vendor
SELECT vendorid, COUNT(*) cnt
FROM vendor_sales
WHERE year = 2019
GROUP BY vendorid, month, day
) vendor_daily_totals
GROUP BY vendor_daily_totals.vendorid
) vendor_avgs;
I'm curious if there is in general a way to compute an average of averages more efficiently.
This is running in Impala, by the way.
I think you can just do the calculation in one shot:
SELECT AVG(t.avgs)
FROM (
SELECT vendorid,
COUNT(*) * 1.0 / COUNT(DISTINCT month, day) as avgs
FROM vendor_sales
WHERE year = 2019
GROUP BY vendorid
) t
This gets the total and divides by the number of days. However, COUNT(DISTINCT) might be even slower than nested GROUP BYs in Impala, so you need to test this.

SQL Query for summation of a difference

I am trying to get the total spending by customers (and their industry types) as (Invoice total- Credit Memo). I tried doing this as a summation of the difference in the SELECT statement with a GROUP BY Cust. Code. But this doesn't work at all. Where am I going wrong
SELECT "CardCode",
"CardName",
"IndName",
(OINV."DocTotal" - ORIN."DocTotal") AS InvoiceTotal
GROUP BY "CardCode"
Appreciate any help!

Issue with finding out a percentage from the average in Postgres

Before I introduce my issue, I must specify that I am a beginner with SQL and Postgres.
I've made a database in Postgres, as a part of a project and I need to interrogate it. The database is about a firm which sells fertilizer.
One of the request is that I need to write a query that will return the Stores with Sales of 25% of the average of the total sales.
I have found out the average of the Sales by using the following query:
SELECT StoreID
FROM Sales
WHERE Price < (SELECT ROUND(AVG(Price)) FROM Sales);
Now, I don't know what should I put in the query to get the result.
Can anyone guide me?
If you mean sales with price below 25% of the average:
select storeid
from (
select storeid, price, avg(price) over() as avg_price
from sales
) s
where price < 0.25 * avg_price