Why my select with an expression in 'where' clause doesn't work? - n1ql

I am trying to do something like
SELECT *
FROM bucket bck
WHERE bck.type = 'Beer'
and ARRAY_LENGTH(bck.path.to.array) > 1
Unfortunately, this select returns nothing.

The predicate might be evaluated to false. Check by running this and see what values it returns
SELECT ARRAY_LENGTH(bck.path.to.array) FROM \bucket bck WHERE bck.type = 'Beer';

Related

Using Case statement in Where clause in SQL developer

I am trying to write a "case" statement inside a "where clause" which has an "in" statement in the "then" part. Basically, the following code is what I want, but it's not the Oracle correct syntax. Does anybody have any idea what the correct syntax should be like?
create or replace PROCEDURE "Test"
(
Type in number
)
as
begin
select Id, Name, AccCode from myTable
where case Type
when 0 then AccCode in (130,131)
when 1 then AccCode in (230,231);
end;
I don't think you want a case statement. You probably just want
where (accCode in (130,131) and type = 1)
or (accCode in (230,231) and type = 0)
If you really want to use a case statement, you could say
where (case when accCode in (130,131) then 1
when accCode in (230,231) then 0
else null
end) = type
But that will not be as readable nor will it be as easy for the optimizer to find a good execution plan.

Create new bit column based on string evaluation

I was looking into the function Contains but it can only be used in the Where predicate, what I'm looking for is something of the sorts
Select doc.* , IsSync = StringContains('Sync', doc.Url) from vw_Doc as doc
IsSync now would contain 1/0 or true/false, depending if the word Sync exists in the document Url.
Is this at all possible ?
Thank you for your time
Edit: StringContains is meant as a pseudo-function, it's not valid syntax
I would use case and charindex().
select
doc.* ,
case
when charindex('Sync', doc.Url) > 0
then 1
else 0
end as IsSync
from
vw_Doc as doc
Contains is not limited to a where clause, it returns a boolean that can be used in various contexts, e.g.:
select *,
case when Contains( doc.url, 'Sync' ) then 1 else 0 end as IsSync
from vw_Doc;

Python and SQLite3 SELECT statement

I was using the SELECT method in a function. I tried to Select all the rows where "First_Name," a column, is equal to "FirstName," a variable. However, it gives me an error saying that "FirstName" is not a column. I have no idea how to fix this issue. I have copy-pasted the SELECT statement below.
c.execute('SELECT * FROM AccountLists WHERE First_Name = FirstName AND Last_Name = LastName')
You need use ? notation, like this:
c.execute('SELECT * FROM AccountLists WHERE First_Name=? AND Last_Name=?', (FirstName, LastName))
The ? is a placeholder that you use wherever you want to use a value and then provide a tuple of values as the second argument to the cursor’s execute() method.

Cascaded Filter in spotfire

I am using cascaded filters in my reports and I have my expression like below -
if ([Region] = "${whichRegion}",[State],null)
along with the above expression, I would like to preselect one of the values of [state] column.
i would need some thing like this -
if ([Region] = "${whichRegion}",[State],null)
or
[State] = 'some default value'
I know above expression throws error.
Could you please let me know how the above expression can be modified ?
Just place your default state value in your false clause.
if([Region] = "${whichRegion}",[State],"DefaultStateValue")
BTW, where are you using this expression at?

Condition IF In Power Query's Advanced Editor

I have a field named field, and I would like to see if it is null, but I get an error in the query, my code is this:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
if field <> null then query1=query1 & " where id = '"& field &"',
exec= Oracle.Database("TESTING",[Query=query1])
in
exec
but I get an error in the condition, do you identify the mistake?
I got Expression.SyntaxError: Token Identifier expected.
You need to assign the if line to a variable. Each M line needs to start with an assignment:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
query2 = if field <> null then query1 & " some stuff" else " some other stuff",
exec= Oracle.Database("TESTING",[Query=query2])
in
exec
In query2 you can build the select statement. I simplified it, because you also have conflicts with the double quotes.
I think you're looking for:
if Not IsNull(field) then ....
Some data types you may have to check using IsEmpty() or 'field is Not Nothing' too. Depending on the datatype and what you are using.
To troubleshoot, it's best to try to set a breakpoint and locate where the error is happening and watch the variable to prevent against that specific value.
To meet this requirement, I would build a fresh Query using the PQ UI to select the students table/view from Oracle, and then use the UI to Filter the [id] column on any value.
Then in the advanced editor I would edit the generated FilteredRows line using code from your Condition + field steps, e.g.
FilteredRows = Table.SelectRows(TESTING_students, each [id] = Excel.CurrentWorkbook(){[Name="test_table"]}{0}[fieldColumn])
This is a minor change from a generated script, rather than trying to write the whole thing from scratch.

Resources