PostgreSQL question - finding sum of sales by state - sql

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

Related

Redshift Rounding Off Issue

I have a table which has a numeric(23,2) field that I need to divide to a constant.
My baseline is this aggregation
select site, sum(sales) / 1.07 as sales from sales group by site;
But when I add another column then compare the total sum across all, I noticed some decimal drop-offs
select site, product, sum(sales) / 1.07 as sales from sales group by site, product;
Is there like a proper way to handle such in Redshift?
I would suggest dividing before doing the sum:
select site, product, sum(sales / 1.07) as sales
from sales
group by site, product;
Mathematically, this should be equivalent. However, because numbers are not infinite precision in computers, this may address your issue.

SUM rows by column unknown parameter - SQL

I've a table like this one
Where each customer has its own budget, and he/she spent it on some fruits. Budget isn't deduplicated by row (so Mike has 20 bucks to spend overall), whilst spent budget is purchase-specific (Mike has now 1 dollar to spend only)
I'd like to add a column that sum the overall spend by each customer in a non deduplicated way.
Like this:
I can't specify any WHERE clause, as I don't know all the names of the customers.
Any idea on how to go from one table to the other?
Thanks,
Carlo
You could use this query to achieve the data you need:
select
customer_name,
customer_budget,
purchase_type,
budget,
customer_budget - sum(budget) as remaining_budget
from Customers
group by customer_name, customer_budget, purchase_type, budget
Hope this helps :)

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.

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

Find highest value in column using ANY or ALL

I'm new to SQL and am currently learning and this is probably a fairly basic question:
I have 4 tables
1) Customer (customerName, street, customerCity)
2) Deposit (customerName, branchName, accountNumber, balance)
3) Loan (customerName, branchName, loanNumber, amount)
4) Branch (branchName, branchCity, assets)
Each table has some data inserted into it.
I have been asked to find the customerName with the highest deposit amount. (So I'm guessing I will be using just the Deposit table)?
However, here is the catch, the sheet I am learning from is requesting that I MUST use either ALL or ANY, I can't simply use the MAX function to achieve this.
How would I achieve this? I've tried query after query and simply can't find a way to get it to work (baring in mind that I've only been learning this for a week).
The things I've been trying have been along the lines of:
SELECT customerName
FROM Deposit
WHERE balance > ALL;
The query should return 1 value, which would be the customerName with the highest balance value.
Thanks a lot for you help :)!
You are looking for the customer(s) whose balance is greater than or equal to all balances in the table
so you just need to use >= instead of >
SELECT customerName
FROM Deposit
WHERE balance >= ALL (SELECT balance
FROM Deposit);
Or you can use a correlated sub query and look for customers whose balances are greater than all other balance values.
SELECT customerName
FROM Deposit d1
WHERE balance > ALL (SELECT balance
FROM Deposit d2
WHERE d2.balance <> d1.balance);
In the event of ties both queries will return all customers with the highest amount.