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

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

Related

Tableau: sum of null rows

I'm trying to create a calculated field that sums rows of data where two columns are both missing data. I'm trying this:
SUM( INT( ISNULL( [Column1] ) AND ISNULL( [Column2] ) ) )
However, this gives me very odd results. In one example, I'm getting a result of 882 with the code above where there are a total of 35 rows, where only 10 rows are missing values. I've tried many variations, I'm always getting way too high numbers. The columns in question contains strings, so ZN() won't work. Changing aliases don't seem to make any difference.
What (probably simple) thing am I missing?
Edit: After comments - removing the sum in the CF and then re-adding it as an aggregation makes no difference. Yes, it is a combination of two database tables. The following is the table view without the sum in the CF:
Red is column 2 and pink is column 1. So the CF definitely shows something different for the blank rows, but I can't figure out why it ends up with 99 and 45 (and why sometimes one and sometimes the other). Ideally, I should be getting just 1s and 0s. Green are unique IDs.

How to get the average of multiple columns with NULL in PostgreSQL

AVG function in PostgreSQL ignores NULL values when it calculates the average. But what if I want to count the average value of multiple columns with many NULL values?
All of below commands dont work
AVG(col1,col2,col3)
AVG(col1)+AVG(col2)+AVG(col3) -> sum calculation alone gives wrong value because of null calculation
This question is similar to this Average of multiple columns, but is there any simple solution for PostgreSQL specific case?

Dividing AVG of column1 by AVG of column2

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.

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.

How do I add another column to a PostgreSQL subquery?

I wasn't too sure how to phrase this question, so here are the details. I'm using a trick to compute the hamming distance between two bitstrings. Here's the query:
select length(replace(x::text,'0',''))
from (
select code # '000111101101001010' as x
from codeTable
) as foo
Essentially it computes the xor between the two strings, removes all 0's, then returns the length. This is functionally equivalent to the hamming distance between two bitstrings. Unfortunately, this only returns the hamming distance, and nothing else. In the codeTable table, there is also a column called person_id. I want to be able to return the minimum hamming distance and the id associated with that. Returning the minimum hamming distance is simple enough, just add a min() around the 'length' part.
select min(length(replace(x::text,'0','')))
from (
select code # '000111101101001010' as x
from codeTable
) as foo
This is fine, however, it only returns the hamming distance, not the person_id. I have no idea what I would need to do to return the person_id associated with that hamming distance.
Does anybody have any idea on how to do this?
Am I missing something? Why the subquery? Looks to me like the following should work to:
select length(replace((code # '000111101101001010')::text,'0',''))
from codeTable
Going from there I get:
select person_id,length(replace((code # '000111101101001010')::text,'0','')) as x
from codeTable
order by x
limit 1
I replaced the min with an order by and limit 1 because there is no direct way of getting the corresponding person_id for the value returned by the min function. In general postgres will be smart enough not to sort the whole intermediate result but just scan it for the row with the lowest value it has to return.