Redshift Rounding Off Issue - sql

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.

Related

list all products and the total sales in dollars for each product. List products in order from highest total sales to lowest

I am trying to get the price to go in dollar amount in SQLite, I have the SQL code but cant figure out how to get it to format to $ amount.
SELECT product, SUM(price) as Total_Price from sales group by product order by SUM(Price) DESC;
I have tried using the print function with no luck, and haven seen another way to do this.
It seems like you are looking for just:
SELECT product, PRINTF('$%,d', SUM(price)) as Total_Price from sales group by product order by SUM(Price) DESC;
or possibly '$%.2f' (You can't have both commas and cents.)
Note that PRINTF was renamed to FORMAT in sqlite 3.38, but the older name PRINTF still works.

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 Server - How calculate # of entities to hit 80% of sum total?

I have a list of companies, their industry, and their annual revenue.
I need to partition the list by industry and figure out how many companies in each industry it takes to account for 80% of the industry's total revenue.
I can run the partition, I can figure out what 80% of each industry's revenue is, but I have zero idea how to figure out how many companies it takes to hit 80%. My only idea is to pull a list for each industry, sort revenue high to low, and sum down until I hit the 80% number.
Are there any built-in functions or clever approaches that can help me here?
Thanks!
I would use window functions:
select industry, count(*)
from (select t.*,
sum(revenue) over (partition by industry order by revenue desc) as running_revenue,
sum(revenue) over (partition by industry) as total_revenue
from t
) t
where running_revenue - revenue < 0.8 * total_revenue
group by industry;
The where includes all companies up to the first that passes the 80% threshold.
There are other functions such as ntile() and percentile() that can be used. I find it simplest to do the calculation directly using sum().

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