How to obtain the value of an expression from a range in SPARQL - range

I have an ontology with different classes and each class has a range with an expression. I'm attacking the ontology with Apache Jena Fuseki but when I execute the query to get the properties with their values I receive "b0" or "b1" instead of the expression of range (this expression could be "xsd:double[>= "0.0"^^xsd:double , <= "20000.0"^^xsd:double]")
The query is:
SELECT ?property ?range
WHERE {
?property rdfs:domain ns:Area;
rdfs:range ?range.
}
And the result is
property range
ns:isSubcapsularLocalized <http://www.w3.org/2001/XMLSchema#boolean>
ns:hasAreaContrastPattern _:b0
I want the query return the expression "xsd:double[>= "0.0"^^xsd:double , <= "20000.0"^^xsd:double]" instead of _:b0
Thanks!

Related

Create a postgresql function that returns a string 'fall', 'spring' depending on year

I need to create a function that returns the string 'fall' or 'spring' depending on the month of the year. If the function was named getterm and took no parameters I would like to use it in a select statement like this:
select name, classname, getterm from classtable
where classtable holds the names of the classes we offer. The result set would include the columns as follows:
-Jack, Data Systems, Sp2022
-Jill, Web Stuff, F2023
I have used the now() function. I can also use extract(quarter from now()) to get my current quarter. It would seem simple then to use an 'if' or 'case' clause to return 'spring' or 'fall' based upon the quarters. I just haven't found any examples of a function like this.
Can anyone suggest some sample code ?
Per documentation from here CASE:
There is a “simple” form of CASE expression that is a variant of the general form above:
CASE expression
WHEN value THEN result
[WHEN ...]
[ELSE result]
END
The first expression is computed, then compared to each of the value expressions in the WHEN clauses until one is found that is equal to it. If no match is found, the result of the ELSE clause (or a null value) is returned. This is similar to the switch statement in C.
This translates in your case to:
SELECT
CASE extract('quarter' FROM now())
WHEN 1 THEN
'winter'
WHEN 2 THEN
'spring'
WHEN 3 THEN
'summer'
WHEN 4 THEN
'fall'
END;
case
--------
spring
Thank you to Adrian.. I now have the working function:
create function getTermString() returns text as $$
select case extract(quarter from now())
when 1 then 'sp'
when 2 then 'sp'
when 3 then 'f'
when 4 then 'f'
end ;
$$ language SQL;

Postgresql - select column based on condition

In this query the 'Daily' in the case will be replaced by a variable. I am not able to make this query work. I want to have the date column being either a day, a week a month or a year based on the value of the variable. but it is giving me various errors..
CASE types date and double precison cannot be matched
syntax error near "as"
what am I doing wrong?
select
case 'Daily'
when 'Daily' then DATE(to_timestamp(e.startts)) as "Date",
when 'Weekly' then DATE_PART('week',to_timestamp(e.startts)) as "Date",
when 'Monthly' then to_char(to_timestamp(e.startts), 'mm/yyyy') as "Date",
when 'Yearly' then to_char(to_timestamp(e.startts), 'yyyy') as "Date",
end
sum(e.checked)
from entries e
WHERE
e.startts >= date_part('epoch', '2020-10-01T15:01:50.859Z'::timestamp)::int8
and e.stopts < date_part('epoch', '2021-11-08T15:01:50.859Z'::timestamp)::int8
group by "Date"
CASE ... END is an expression. An expression must have a well-defined data type, so PostgreSQL makes sure that the expressions in the THEN clause have the same data type (or at least compatible ones).
You would need a type cast, probably to text, in the first two branches:
... THEN CAST (date(to_timestamp(e.startts)) AS text)
But it would be much better to use to_char in all branches – there are format codes for everything you need.
An expression can have no alias, only an entry in the SELECT or FROM list can. So you need to append AS "Date" at the end of the CASE ... END expression, not somewhere in the middle.

How use '?' in #Query? How use Jsonb of postgres in spring-boot at all?

How to use ?| operator in postgres query in spring repository? I need to use where in my query for text type column which content json.
#Query(value =
"SELECT * \n" +
"FROM tbl t \n" +
"WHERE t.some_ids::::jsonb ?| array['152960','188775']", nativeQuery = true
)
List<Model> getModelsByIds();
But that don't work and I catch the next exeception:
org.springframework.dao.InvalidDataAccessApiUsageException: At least 1 parameter(s) provided but only 0 parameter(s) present in query.
You can use the associated function of that operator instead. Most of the time the obfuscation layers also choke on the :: cast operator, so you might want to use cast() instead:
WHERE pg_catalog.jsonb_exists_any(cast(t.some_ids as jsonb), array['152960','188775'])
However I think this wouldn't be able to make use of an index defined on some_ids::jsonb
You didn't mention how exactly the content of some_ids looks like.
If that is a JSON array (e.g. '["123", "456"]'::jsonb) then you can also use the contains operator #>:
WHERE cast(t.some_ids as jsonb) #> '["152960","188775"]'
If your JSON array contains numbers rather than strings ('[123,456]') you need to pass numbers in the argument as well:
WHERE cast(t.some_ids as jsonb) #> '[152960,188775]'

Dynamically constructing a SQL request parameter in mybatis

Below is my query
SELECT
/*+ ORDERED */
F.*,
SDO_NN_DISTANCE(1) dist
FROM NEW_TABLE F
WHERE SDO_NN(F.LOC_GEOM, SDO_GEOMETRY( 2001, 8307, SDO_POINT_TYPE(-12.1254, 22.1545,NULL), NULL, NULL ), 'SDO_BATCH_SIZE=0 DISTANCE=60 UNIT=MILE', 1)='TRUE'
ORDER BY dist;
In the above query the value of distance will be changing.
'SDO_BATCH_SIZE=0 DISTANCE=60 UNIT=MILE'
So can I construct the request parameter dynamically by adding the value eg., 60 to the parameter using mybatis/ibatis?
Using a simple Oracle concatenation operator "||" answered my question.
Replaced 'SDO_BATCH_SIZE=0 DISTANCE=60 UNIT=MILE'
with the below in the mybatis query
'SDO_BATCH_SIZE=0 DISTANCE=' || #{input_distance} || ' UNIT=MILE'

EF 4.1 Linq Predicate Contains Generates desired SQL when using hard-coded string but not with string variable

I have this code:
string name ="M";
Expression<Func<Organization, bool>> getExpression1 = r => r.Name.Contains(name);
Expression<Func<Organization, bool>> getExpression2 = r => r.Name.Contains("M");
GetOrgListBy(getExpression);
GetOrgListBy(getExpression2);
public IEnumerable<Organization> GetOrgListBy(Expression<Func<Organization, bool>> filter)
{
return DataContext.Parties.OfType<Organization>().Where(filter).ToList();
}
Question:
I am passing a simple expression to a repository function
that gets List<Organization> by matching the name passed.
When I view the sql trace for getExpression1 (uses a string variable), the expression generates a [Name] like plinq_variable sql statement
which omits the % operator. My intention is to generate a
[Name] like '%' + plinq_variable + '%' sql statement
However, when I when using getExpression2 (hard-coded the string), the expression successfully
generates a [Name] like N'%M%' sql statement.
Why doesn't getExpression1 generate the % operator?
How can I make it generate a % operator in the sql just like getExpression2?
The first expression translates into a parametrized SQL query. The variable in the Where clause ...
[Name] like plinq_variable
... is set to the value
#plinq_variable=N'%M%'
when the query is executed. The second expression translates to a SQL query with a hardcoded string literal:
[Name] like N'%M%'
Then this query is directly executed.
The result for both expressions and queries is the same.