Tableau: sum of null rows - tableau-api

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.

Related

How do I rank the column on percentile and remove the bottom 0.1% percentile data from a column in PostgreSQL?

At work I need to remove the bottom 0.1% data aka I only need data with percentile rank =< .99.
How can I rank the column I want to calculate percentile on where it gives me percentile rank from .01 to .99 so that I can eliminate the data I don't want?
The column I want to get the percentile for also needs to be partitioned by another column. There are X values in one column, which has Y values each which I want the percentile for.
I used the percent_rank function, but it doesn't give out accurate results. The examples on the internet show it'll rank the data from 0 to 1 but while mine does start at 0 it ends at .57 for one column and .93 for another column and so on, but never goes till .99 or 1.
I wrote percent_rank() over (partition by ColX order by ColY). Am I missing something here? If this works properly, it's exactly what I am looking for.
I also tried using the functions shown here, but I didn't quite understand what's happening with the ntile function and the generate_series returned an error basically saying that the numbers in the brackets (0.01, 1, 0.01) are out of range. The host cloud tool my company uses doesn't work with Postgres, like Postgres now accepts -1 indexing, but the tool we use doesn't. It still says indexing needs to be a positive number. So I don't exactly know why the error is occuring.
I feel like I am missing something obvious here, there is a very simple function which will do the work but I can't just find it.
It looks like values in ColY column are not unique inside ColX group.
In this case last records in each group share the same percent rank
In example below ident is unique inside group
Question without unique "order by"
select ident, ColX, ColY, percent_rank() over (partition by ColX order by ColY) from table
Result
ident
ColX
ColY
percent_rank
1
1
1
0
2
1
2
0.5
3
1
2
0.5
Question with unique "order by"
select ident, ColX, ColY, percent_rank() over (partition by ColX order by ColY, ident) from table
Result
ident
ColX
ColY
percent_rank
1
1
1
0
2
1
2
0.5
3
1
2
1

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.

Delete rows from a Table when 2 specific columns are both zeros

I'm trying to delete rows of a table, when both values of 2 specific columns are equal to zeros. I've tried to use ismember(col1 & col2, 0),:)=[]; but it deletes the rows when only one of the column is zero.
Ideally, i would also like to do the opposite: delete every rows where the cells of these 2 columns aren't both zero.
I know it would be easier if I wasn't using a table, unfortunately there is some needed variables that aren't numeric.
It would be great if you know a way to do what i need with a table.
Cheers

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 to select 30 questions from questions table with the condition like sum of marks should be 120

I have table containing columns like
Now my requirement is to fetch 30 random questions and also the total of the marks should be 120.
I know how to fetch 30 random questions i.e. by using following query:
select * from questions order by random limit 30
I also know how to find sum of the marks column i.e. by using following query:
select sum(marks) from questions
But I am stuck at how to check condition that the 30 random questions that i am bringing are those whose marks column total is 120.
One way I thought of tackling this (without getting into recursion or looping until you get the right total) is that if you know the spread of the marks, you could pull out random lists for a set number of each.
Eg. (marks*n) + (marks*n) + (marks*n) = 120 aka (4*20)+(5*8) = 120 marks
and in SQL :
select * from
(
select * from questions where marks = 4 order by random limit 20
union all
select * from questions where marks = 5 order by random limit 8
) a
I suppose you could even derive a mathematical formula to randomize the spread to give you a truly random selection.
Hope this helps.
Paul P
Your problem is the knapsack problem (where weight and value are equal for all objects).
Solving it practically impossible in SQL; you should just read all question marks and implement the solution in your code.