Dividing AVG of column1 by AVG of column2 - postgresql

I am trying to divide the average value of column1 by the average value of column 2, which will give me an average price from my data. I believe there is a problem with my syntax / structure of my code, or I am making a rookie mistake.
I have searched stack and cannot find many examples of dividing two averaged columns, and checked the postgres documentation.
The individual average query is working fine (as shown here)
SELECT (AVG(CAST("Column1" AS numeric(4,2))),2) FROM table1
But when I combine two of them in an attempt to divide, It simply does not work.
SELECT (AVG(CAST("Column1" AS numeric(4,2))),2) / (AVG(CAST("Column2" AS numeric(4,2))),2) FROM table1
I am receiving the following error; "ERROR: row comparison operator must yield type boolean, not type numeric". I have tried a few other variations which have mostly given me syntax errors.

I don't know what you are trying to do with your current approach. However, if you want to take the ratio of two averages, you could also just take the ratio of the sums:
SELECT SUM(CAST(Column1 AS numeric(4,2))) / SUM(CAST(Column2 AS numeric(4,2)))
FROM table1;
Note that SUM() just takes a single input, not two inputs. The reason why we can use the sums is that average would normalize both the numerator and denominator by the same amount, which is the number of rows in table1. Hence, this factor just cancels out.

Related

T-SQL : Calculating the % of rows that are not null

So I must be daft - how would one calculate the number of rows that contain a value for addressline2? aka the % of rows where Addressline2 is not null.
SELECT
(count(addressline2)/count(*))*1.000
FROM
AdventureWorks2014.person.address
I thought that would do it, but it gives 0.
So I tried using common table expressions to grab a count of both scenarios, then divide them. Also tried using a subquery to do the same.
This seem so simple - what am I missing?
You just have the 1.0 multiplier in the wrong spot.
SELECT
(1.0 * count(addressline2)/count(*))
FROM
AdventureWorks2014.person.address

Tableau: Finding an attribute count from ONLY distinct rows

I can't seem to find a solution for my issue anywhere, and I hope that I can ask my question correctly here to find an answer:
I am having a problem finding a count of the number of values from only the distinct rows in my dataset.
In my Tableau sheet, I am trying to find the number of cases that are closed as "FCR" - and in the database, this is represented as true or false.
My formula:
SUM(if [IsFCR] = true then 1 else 0 end)
The problem that I am running into is that this is getting the count from all of the rows in my data. But I need represent the total cases, and the FCR value from each of THOSE rows as a distinct count.
As seen in this image: Screen capture of Tableau formula
I am returning 9,000 distinct rows - but 46,000 counts of FCR being true.
Notably, I have tried wrapping my formula in a COUNTD (and it returns 2), in Window_Count (returns 1), among many other guesses as to how I could wrap this calculated field in a way to only count the unique rows.
If only I could use a foreach logic...
Any help that you can give is greatly appreciated.

PgSQL - Error while executing a select

I am trying to write a simple select query in PgSQL but I keep getting an error. I am not sure what I am missing. Any help would be appreciated.
select residuals, residuals/stddev_pop(residuals)
from mySchema.results;
This gives an error
ERROR: column "results.residuals" must appear in the GROUP BY clause or be used in an aggregate function
Residuals is a numeric value (continuous variable)
What am I missing?
stddev_pop is an aggregate function. That means that it takes a set of rows as its input. Your query mentions two values in the SELECT clause:
residuals, this is a value from a single row.
stddev_pop(residuals), this is an aggregate value and represents multiple rows.
You're not telling PostgreSQL how it should choose the singular residuals value to go with the aggregate standard deviation and so PostgreSQL says that residuals
must appear in the GROUP BY clause or be used in an aggregate function
I'm not sure what you're trying to accomplish so I can't tell you how to fix your query. A naive suggestion would be:
select residuals, residuals/stddev_pop(residuals)
from mySchema.results
group by residuals
but that would leave you computing the standard deviation of groups of identical values and that doesn't seem terribly productive (especially when you're going to use the standard deviation as a divisor).
Perhaps you need to revisit the formula you're trying to compute as well as fixing your SQL.
If you want to compute the standard deviation separately and then divide each residuals by that then you'd want something like this:
select residuals,
residuals/(select stddev_pop(residuals) from mySchema.results)
from mySchema.results

Interactive Report - aggregate sum of multiple columns from one table multiply by values from another Table

I have a challenge in Oracle Apex - I would to sum multiple columns to give 3 extra rows namely points, Score, %score. There are more columns but I'm only choosing a few for now.
Below is an example structure of my data:
Town | Sector | Outside| Inside |Available|Price
Roy-----Formal----0----------0----------1------0
Kobus --Formal----0 ---------0--------- 1------0
Wika ---Formal----0----------0--------- 1------0
Mevo----Formal----1----------1----------1------0
Hoch----Formal----1----------1----------1------1
Points------------2----------2----------5------1
Score------------10---------10---------10------10
%score-----------20---------20---------50------10
Each column has a constant weighting (which serves as a factor and it can change depending on the areas). In this case, the weighting for the areas are in the first row of the sector Formal:
Sector |Outside| Inside |Available|Price
Formal----1----------1 ----------1-----1
Informal--1----------0 ----------2-----1
I tried using the aggregate sum function in apex but it wont work because I need the factor in the other table. This is where my challenge began.
To compute the rows below the report
points = sum per column * weighting factor per column
Score = sum of no of shops visited (in this case its 5) * weighting factor per column
% score = points/Score * 100
The report should display as described above. With the new computed rows below.
I kindly ask anyone to assist me with this challenge as I have tried searching for solutions but haven't come across any.
Thanks a lot for your support in advance!!

Getting the Sum of the Contents of Rows

I have a column with dollar amounts, such as:
$1.00
$4.00
$100.00
and so on. These values are put into the column by calculating the % of another column. I want to get the sum of all the dollar amounts of that column. I cant use Sum(data) because its static data in the column. Is there a way to get the sum easily?
Thanks!
There are a couple of different ways to deal with this:
If it's the same percentage being applied to each row, sum the original column and apply the same percentage to that sum.
If the original source of the data is from a database, do the percent calculation in your Dataset's SQL query, then use the Sum function in your Tablix as usual.
If these don't apply, you'll need to give more detailed information about your problem.