return nth value in ranked numeric field using Qlikview - min

I want to return the nth value in a numeric field in a Qlikview chart. The field is not sorted in the load. I want n to be an expression.
I have tried using min(FieldName, round(expression)) but the offest value is not recognised and the 1st minimum value is returned.
Is there a way round this that allows me to use an expression to determine the value of n?

You need to use the Rank function. It specifically works on expressions and you can also specify how the rank behaves (min, max, avg).
rank([ total ] expression [ , mode [, format ] ])
There are also variants like HRank and VRank but they work in specific charts.
Then simply pick the nth value from the rank like
pick(n, rank())

Related

How can I use an SQL aggregate function on data I directly input at the command line (e.g. AVG(1, 2, 3))?

How can I enter multiple values into an aggregate function using just data I enter at the command line? Say, in Postgres, I run the following.
SELECT AVG(2);
I'll get the correct answer, but I can't find a way to enter multiple values, such as below, without getting an error.
SELECT AVG(1,NULL,2,3);
I've tried wrapping the numbers in various brackets but to no effect. What's the syntax I'm missing?
EDIT: Additionally, is there a way to include NULLs in the input?
AVG() is an aggregate that operates over multiple rows. So you need to convert your comma separated list to one row per value to be able to use an aggregate like avg(). This could be done using e.g. string_to_table
select avg(num::numeric)
from string_to_table('1,2,3', ',') as x(num)
If you want to include a NULL value, you could add it to the list and convert it to null before casting it to a numeric value:
select avg(nullif(num, 'null')::numeric)
from string_to_table('1,2,3,4,null', ',') as x(num)

Filter by array length in typesense query

I am using typesense and have the following document structure in a people collection:
{
...
people: ['Jim', 'Jane', 'Jack']
...
}
I would like to add a filter to my query that allows me to filter by the number of entries within the peoples array for each document.
e.g
{
filter_by: people:=[0...2]
}
This should return all documents that have between 0 - 2 entries within the peoples array.
The documentation states that this is possible for numeric fields, but does not mention anything related to array fields.
Numeric Filtering:
Filter documents with numeric values between a min and max value, using the range operator [min..max] or using simple comparison operators >, >= <, <=, =.
Is this possible or do i need to explicitly add in a numeric field to the documents.
It's not possible to filter based on array length in Typesense.
So you would have to create a new numeric field in each document called say people_length, that you calculate at indexing time and then use that field for filtering.

Count the number of times the portion of a link beginning with certain string appears in the text of a column

I need to count for each row the number of times the portion of a link beginning with 'https://t.co/' appears in the text of a column named "Tweet_text".
I've done:
SELECT COUNT(REGEXP_CONTAINS('https://t.co/', Tweet_text)) As Cnt
FROM `MyTable`
But this returns the overall count over the whole table, not the count row by row.
You can try this query:
SELECT ARRAY_LENGTH(REGEXP_EXTRACT_ALL(Tweet_text, 'https://t.co/'))
FROM MyTable
The function REGEXP_CONTAINS only returns the state whether your regular expression was found:
Returns TRUE if value is a partial match for the regular expression, regexp.
If you want to get the count of found substring in your column you have to use REGEXP_EXTRACT_ALL with ARRAY_LENGTH.
You get the count of each row (not a sum) because you don't use a aggregate function (like COUNT) anymore.

Exclude rows when column contains a 1 in position 2 without using function

I have a column that will always be 5 digits long, and each digit will always be a 1 or a 0. I need to put in my where clause to exclude when the second position is equal to 1. For example 01000 is to be excluded but 10010 is to be kept. I currently have:
WHERE (SUBSTRING(field, 2, 1) <> '1') or field IS NULL
How do do this without using the Substring function?
Edit:Also, the column is a varchar(10) in the database. Does this matter?
You could use the like operator to check that character directly:
WHERE field LIKE '_1%' OR field IS NULL
Use LEFT and RIGHT and then check that is 1 or not as below-
WHERE RIGHT(LEFT(field,2),1) <> '1' OR field IS NULL
No.
If 'field' is of a string type, you need to use string functions to manipulate it. SUBSTRING or some other flavor of it.
You can also convert it to binary and use bitwise AND operator but that won't solve the root issue here.
You are facing the consequences of someone ignoring 1NF.
There is a reason why Codd insisted that every "cell" must be atomic. Your's is not.
Can you separate this bitmap into atomic attribute columns?

SQL and iReport - Aggregate functions in the Where clause and Parameters

Well basically I would like to be able to use a parameter on iReport with an aggregate function.
If you type "yes" it will show you the values greater than 0, if you type "no" it will show the values that are less than 0. However, the aggregate function first adds up all the values related to an id and then it subtracts the result from another value, the result of that is the one I want to show.
How would I be able to do this? I'm clueless as I don't know how to use it with HAVING.
I don't understand what 'it' refers to in "I don't know how to use it with HAVING." The question will be much clearer with some SQL. But I guess you're looking for this:
SELECT id, sum(values) as the_agg
FROM table1
GROUP BY id
HAVING sum(values) $P!{BiggerOrSmaller} 0
The default value for the parameter BiggerOrSmaller should be like this:
$P{MyParam}.equals("yes") ? ">" : "<"
This assumes you have a parmeter called MyParam which can take the value "yes". Based on that value it sets the parameter BiggerOrSmaller appropriately.