Making SPARQL FILTER conditional - sparql

How to make a FILTER() conditional? This is the relevant part of my query:
SELECT *
WHERE {
VALUES (?open) {$U2}
?URI_OPP CSV:id_opportunita ?ID_OPP.
OPTIONAL { ?URI_OPP CSV:data_scadenza ?DATA_S }
FILTER ((NOW() - xsd:datetime(?DATA_S)) > 0)
}
It gets $U2 as a value for ?open. I want to apply the filter if ?open = 1, and not to apply it in all other cases.
While IF() works on the query results, I don't know what to use to switch off parts of the query itself.

Since the above commented never answered, I will:
FILTER(
(?open != 1) ||
((NOW() - xsd:datetime(?DATA_S)) > 0)
)

Related

How to use NVL or COALESCE in native query for list of values in Spring Data JPA

I am using native query as follows which fetches data from multiple tables.
I also want to perform filtering on it based on users input. User can select either none, one or multiple checkboxes in filter menu based on which my query should return the record.
I have tried both the approaches like:
src_region.region_id NVL(:regionIds,src_region.region_id )
src_region.region_id in(:regionIds) OR COALESCE (:regionIds)IS NULL)
But for both of them I am getting errors as I am sending List values.
public interface dbRepository
{
public static final String dbQuery = "select * from
(select src_data.*,
trg_data.*
from
(select reg.region_id,reg.region_name,
ctry.country_id,ctry.country_name
from src_region reg,src_ctry ctry
where reg.region_id=ctry.region_id
AND src_region.region_id in(:regionIds) OR COALESCE (:regionIds)IS NULL) src_data,
(select md.module_id,reg.module_name,
ctry.country_id,ctry.country_name
from trg_module md,trg_ctry ctry
where md.module_id=ctry.country_id
AND ctry.country_id in(:countryIds) OR COALESCE (:countryIds)IS NULL) trg_data
ORDER BY src_data.region_id ";
#Query( value = dbQuery, nativeQuery = true )
List<Object[]> findBySorceOrTarget( #Param( "regionIds" ) List<Long> regionIds, #Param( "countryIds" ) List<Long> countryIds );
}
User may on may not send values. Also, rather than sending multiple values, they can also send only one value. What is the better way to pass list of values to parameters in native query?
Generally speaking, that should be
AND (src_region.region_id = :regionIds or :regionIds IS NULL)
However, if you can select multiple values, then = won't be OK - you'll have to switch to in (as code you posted suggests). Though, you can't do it that way as you'll get nothing as a result - you'll have to split those values into rows.
Something like this (presuming that values in :regionIds are comma-separated, such as 1,2,5:
and (src_region.region_id in (select regexp_substr(:regionIds, '[^,]+', 1, level)
from dual
connect by level <= regexp_count(:regionIds, ',') + 1
)
or :regionIds is null)

The best conditional logic for match and match each case

so, I want to use conditional logic in my code, which the condition is when I got response.response_code == '00' so it will run
And match response == res_3[0]
And match each response.data.bills == res_3[1]
And if response.response_code != '00' , it will run
And match response == res_3
And match each response.data.bills == res_3
so, what is the best conditional logic for this case ??
Read the docs please: https://github.com/intuit/karate#conditional-logic
Use a second feature file:
* eval if (response.response_code != '00') karate.call('it-will-run.feature')
Note: you can't use match where JavaScript is expected, refer: https://stackoverflow.com/a/53961806/143475

Using condition in Calculatetable ()

I've a problem on Table filtering while using CALCULATETABLE()
I tried to use the script with condition for CALCULATETABLE():
XeroInvoices[AmountPaid] < XeroInvoices[AmountDue]
EVALUATE
SUMMARIZE(
CALCULATETABLE(XeroInvoices,
XeroInvoices[Status] = "AUTHORISED",
XeroInvoices[DueDate] <= TODAY(),
XeroInvoices[AmountPaid] < XeroInvoices[AmountDue]
),
XeroInvoices[Number],
XeroInvoices[Reference],
XeroInvoices[Status],
XeroInvoices[Date],
XeroInvoices[DueDate],
XeroInvoices[AmountPaid],
XeroInvoices[AmountDue]
)
but the error that i get in DAX Studio is as following:
Query (6, 30) The expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression.
I managed only to kinda achieve that I wanted only like this -- crating new column within SUMMARIZE() syntax and later filtering it in Excel:
EVALUATE
SUMMARIZE(
CALCULATETABLE(XeroInvoices,
XeroInvoices[Status] = "AUTHORISED",
XeroInvoices[DueDate] <= TODAY()
),
XeroInvoices[Number],
XeroInvoices[Reference],
XeroInvoices[Status],
XeroInvoices[Date],
XeroInvoices[DueDate],
XeroInvoices[AmountPaid],
XeroInvoices[AmountDue],
"AmPaid<AmDue",XeroInvoices[AmountPaid]< XeroInvoices[AmountDue]
)
Does anyone know what might be the reason for this Err in CALCULATETABLE() and what might be a proposed solution?
Thanks!!
Check this
To filter by multiple columns you have to explicitly specify the "FILTER"
CALCULATETABLE (
Product,
FILTER (
Product,
OR ( Product[Color] = "Red", Product[Weight] > 1000 )
)
)

SQL to MDX conversion

I have this where clause in sql language:
where (cond1=1 or cond2=1) and cond3=1
How can I get this result in MDX with the slicing(condition into the where)?
{[cond1].&[1],[cond2].&[1]} /*and*/ {[cond3].&[1]}
Thanks
Try to use a subcube:
Select
-- YOUR SELECTED MEASURES AND DIMENSIONS
From
(
Select
{[cond1].&[1],[cond2].&[1]} on 0
,{[cond3].&[1]} on 1
-- ,{more slices} on x
From [CubeName]
)
Hope this help!
You can use subcube expression as stated above, but this is not the only option. If you use subcube, you would increase query performance greatly (assuming the fact you don't perform crossjoins in it).
You can also use general WHERE keyword after last expression that returns cube:
select
{ .. } on 0,
{ .. } on 1
from (select { [Dim1].[X].allmembers } on 0)
where ([Dim2].[Y].&[Y1])
Or:
select
{ .. } on 0,
{ .. } on 1
from (select { [Dim1].[X].allmembers } on 0)
where {[DimTime].[Time].[Year].&[2001] : [DimTime].[Time].[Year].&[2015]}
This is applied at the end of execution, which means performance may decrease. However, if you need to apply external filter to all axis, this is the option you need.
Another way to filter member values is using tuple expressions:
with member LastDateSale as ( (Tail(EXISTING [DimTime].[Time].[Dates].members,1), [Measures].[ActualSales]) )
This will take your DimTime axis, apply external filter, get the last element from it and calculate [ActualSales] for it, if possible.

Alternative to relying on execution order of conditions in SQL 'where' clause

In languages such as JavaScript you can have 2 conditional statements and "protect" the second one with the first one. For instance:
if( scarryObject != null && scarryObject.scarryMethod() ) { ... }
// if scarryObject is null scarryMethod will not be called
I thought I would achieve the same in SQL like so:
where int_date > 19500101
and month(CONVERT(smalldatetime, ... int_date))
The problem here is that if int_date is some "bad" value like -1, 0, 1 the conversion will fail and the sp will stop with an error. I thought the first check int_date > 19500101 would get evaluated first and if false the second condition would be skipped.
It seems like it does not work like this... or? Is there any other way of doing this?
Thanks!
Your query is syntactically not correct, as the clausemonth(CONVERT.... is not a condition.
Let's assume you want to compare with a certain number, a possible way of expressing what you want would be
SELECT *
FROM myTable
WHERE case
when int_date > 19500101
then -1
else month(CONVERT(smalldatetime, ... int_date))
end = #YourMonth
You would 'protect' the evaluation of the 'month' and not the condition.
You could try splitting the query into two. Here is the concept:
SELECT *
FROM (
SELECT *
FROM myTable
WHERE int_date > 19500101
) t
WHERE month(CONVERT(smalldatetime, ... t.int_date))