Order By number as if it is a string in XQuery - xquery

Using BaseX 8.4.1 which implements XQuery 3.1
I want to order my query result by a certain attribute which contains numbers. Because I need to synchronize this result with a second dataset that stores the corresponding values as strings, I need to sort it as if it were strings, i.e., the values 1,3 and 20 should be sorted like this:
1
20
3
I tried order by $x[string(#value)] and order by string($x[#value]) but that doesn't work.

In both examples you place part of the expression in a predicate ([]), which evaluates to a boolean and returns the expression before the predicate ($x), when true.
order by $x/#value/string()

Related

Perform operation on all substrings of a string in SQL (MariaDB)

Disclaimer: This is not a database administration or design question. I did not design this database and I do not have rights to change it.
I have a database in which many fields are compound. For example, a single column is used for acre usage for a district. Many districts have one primary crop and the value is a single number, such as 14. Some have two primary crops and it has two numbers separated by a comma like "14,8". Some have three, four, or even five primary crops resulting in a compound value like "14,8,7,4,3".
I am pulling data out of this database for analytical research. Right now, I am pulling columns like that into R, splitting them into 5 values (padding nulls if there aren't 5 values), and performing work on the values. I want to do it in the database itself. I want to split the value on the comma, perform an operation on the resulting values, and then concatenate the result of the operation back into the original column format.
Example, I have a column that is in acres. I want it in square meters. So, I want to take "14,8", temporarily turn it into 14 and 8, multiply each of those by 4046.86, and get "56656.04,32374.88" as my result. What I am currently doing is using regexp_replace. I start with all rows where "acres REGEXP '^[0-9.]+,[0-9.]+,[0-9.]+,[0-9.]+$'" for the where clause. That gives me rows with 5 numbers in the field. Then, I can do the first number with "cast(regexp_replace(acres,',.*%','') as float) * 4046.86". I can do each of the 5 using a different regexp_replace. I can concatenate those values back together. Then, I run a query for those with 4 numbers, then 3, then 2, and finally the single number rows.
Is this possible as a single query?
Use a function to parse the string and to convert it to desired result. This will allow for you to use a sigle query for the job.

Express conditions on two consecutive variable length relationships?

How to express a conditions for two consecutive variable length relationships?
Consider this partial query
MATCH(t1:Type{myID: 1})-[r:relType]->(:Type)-[rels:relType*0..]-(t2:Type{myID:100})
WHERE r.attr1>10
Basically I am trying to saying that there could be one or more relations from t1 to t2. The first relation r should satisfy a given condition on its attribute.
If this is the only relation between the two nodes then it's ok.
It at least another relation exist I want to add another condition such as:
WHERE r.attr1>10 AND r_next.attr2> r_prev.attr2+r_prev.attr1
where r_next and r_prev are consecutive relations: ()-[r_prev]->()-[r_next]-(). Note that at the first step r_prev is the first relation r.
I know rels is a collection but I do not know how to express such a condition.
Consecutive comparison like this isn't easy at this time, and it can't currently be evaluated during expansion.
You can do some filtering on this after, but it will be ugly.
We'll make use of the APOC Procedures for apoc.coll.pairsMin(), which takes a collection and returns a list of adjacent pairs.
MATCH (t1:Type{myID: 1}), (t2:Type{myID:100})
MATCH (t1)-[r:relType]->(:Type)-[rels:relType*0..]-(t2)
WHERE r.attr1>10
WITH t1, t2, apoc.coll.pairsMin(rels) as pairs
WHERE all(pair in pairs WHERE pair[0].attr1 + pair[0].attr2 < pair[1].attr2)
RETURN t1, t2 //or whatever you want to return from this

Conditional formatting if a datetime value is present in a list of datetime values

I am trying to compare two lists of DateTime values in a Google Sheets. I want to highlight all DateTimes in the first list which are also present in the second list.
I already tried to use MATCH(), COUNTIF(), FILTER() together with COUNTA() or other approaches. However, although the values in both lists are basically copies of each other (just with some missing values in the second list), no matches will be returned. All are "true" DateTime values which can be formatted by using any of the date and time formatting options.
The MATCH example:
=ISNUMBER(MATCH(A2,INDIRECT("OTHERSHEET!$A$2:$A"),0))
I assume that there might be differences in the DateTime interpretation in the comparison why I also tried to use N() without success. I will always get an error like Did not find value '43297.75867' in MATCH evaluation. respectively FALSE after the ISNUMBER().
If I just do something like =N(A1)=N(OTHERSHEET!A1) with matching DateTimes, I get TRUE.
Same principle, but shorter:
=MATCH(A1,INDIRECT("OTHERSHEET!A:A"),0)
custom formula: =ISNUMBER(MATCH(A1,INDIRECT("OTHERSHEET!$A$1:$A"),0))

Optimize contains query of numbers to exact match query

I am looking to optimize my contains query. I have a pipe separated list of numbers in one of my Aerospike bins(columns) something like 234|235|236|
These numbers may vary from 1 to 2^14
Currently I am applying a contains query to find 235| in this column but it is getting slow. Is there any Math or any strategy I can apply to convert this contains query to an exact match??
TIA,
Karan
Did you try using a List type for this bin? You can then build a secondary index on the List values (indextype = LIST, type=NUMERIC)and get all records that match the value of interest in the list using a secondary index query.

How do I match on three values / keywords in a full text search?

I am using a query similar to the one below:
SELECT id, description
FROM FullTextTable
WHERE FullTextTable MATCH '1.010 OR 1.01 OR 1.0100'
If I only use two 'OR' operators, the query returns as expected, but I get no results when I attempt to use three or more 'OR' operators. How would you phrase a query to check for any of three or more values?
This is using the Sqlite FTS4 standard full text query syntax, which I believe does not support changing the order of operations with the use of parenthesis.
I have descriptions which contain decimals values at an arbitrary precision level and our users will not necessarily search at the same precision level as what is stored in the descriptions (e.g. a user may want to search for 1.010, but the description is actually worded as 1.01 or 1.0100 and the user won't know this ahead of time).
With the default tokenizer, periods are not part of words.
Your query is the same as
... MATCH '1 010 OR 1 01 OR 1 0100'
Use phrase queries instead:
... MATCH '"1.010" OR "1.01" OR "1.0100"'

Resources