Netezza "not exists" within a CASE statement - subquery

I have a multi-layered CASE statement, and one of the conditions needs to reference a table via a "not exists". I keep getting the error about 'correlated subqueries not allowed". How can I reference a table along with a condition inside a CASE statement? Below is a portion of my code:
WHEN ...... previous condition
WHEN ( CCOB_CLIENT_LOB_ID = 2 AND OI_CARRIER_LOB_ID IN (1,2,12,13) )
and not exists ( select S.STATE
FROM CCOB_PACIFICSOURCE.V_SELFPAY_COB_STATES S
WHERE S.STATE = SELFPAY_COB_STATE ) then 'NONE'
WHEN .... subsequent condition

The short answer is: you cann’t.
The longer answer is that you have to rewrite the query to outer join the table you give the alias S.
Then it’s quite possible to test for NULL.
Watch out for dublicates on the S.state column though :)

Related

Conditional Column: Order of Tests Matters?

Condition to Test Date
I am using Power Query to create a status column that checks the date against a specified date, like so:
However, this gives me the following error:
Expression.Error: We cannot convert the value null to type Logical.
Details:
Value=
Type=[Type]
The column does contain empty cells, which I want to report as "null" in the new column. I then tried the following logic, and it errors out as well:
Then I moved the null test to the top, and it finally works:
Why Does Order Matter?
Why does the third query produce the expected results but not the first one? This seems bizarre to me, so if there is something I am missing please let me know.
M is using lazy evaluation in the if statement. If the first statement is true, then it doesn't even bother evaluating the other conditions.
https://learn.microsoft.com/en-us/powerquery-m/m-spec-introduction
For computer language theorists: the formula language specified in
this document is a mostly pure, higher-order, dynamically typed,
partially lazy functional language.
Easy fix
On a step before your filter, choose "remove nulls" or "replace nulls with values"
using catch
If you want more flexibility, you can use a try + catch pair.
Step FirstTry is meant to; be your filter, then I added two ways to handle errors.
let
Source = Table.FromList(sample, Splitter.SplitByNothing(),
type table[Date = nullable date], null, ExtraValues.Error),
sample = {
#date(2020, 1, 1),
"text", null,
#date(2024, 1, 1)
},
filter = #date(2022, 1, 1),
FirstTry = Table.AddColumn(
Source , "Comparison", each filter > [Date], Logical.Type),
WithFallback = Table.AddColumn(FirstTry, "WithFallback",
each try
filter > [Date]
catch (e) => e[Message], type text),
WithPreservedDatatype = Table.AddColumn(WithFallback, "PreserveColumnType",
each try
filter > [Date]
catch (e) => null meta [ Reason = e[Message] ],
type logical)
in
WithPreservedDatatype
things to note
the query steps are "out of order", which is totally valid. ( above sample was referenced "before" its line )
Errors are propagated so an error on step4 could actually be step2. Just keep going up until you find it.
the schema says column [Date] is type date -- but it's actually type any.
What you need is to call Table.TransformColumnTypes to convert and assert datatypes
= Table.TransformColumnTypes( Source,{{"Date", type date}})
Now row 2 will correctly show an error, because text couldn't convert into a date
Better Understanding of NULLs
I was not understanding how Excel (or any other data tool) handles null values and how logical tests are performed on null values. This response on Reddit really helped clarify this in my mind:
https://www.reddit.com/r/excel/comments/xu37dr/comment/iqtmivn/?utm_source=share&utm_medium=web2x&context=3
In short, logical tests involving nulls do not behave like you would expect.
For a deeper dive into this, read this excellent post:
https://bengribaudo.com/blog/2018/09/13/4617/power-query-m-primer-part9-types-logical-null-binary
My Solution
Given this enlightened understanding of nulls and how they behave in logical tests, I now know that I must either:
Convert the null values to empty strings ("") before the query
Test first for nulls in the query
My choice is to test for nulls first within the query, like so:

NetSuite Case when statement on joined

I'm new to writing case statements in NetSuite and would appreciate any input with this. I'm trying to create following statement within item search, but receiving invalid expression error.
CASE WHEN {transaction.status} = "Purchase Order:Pending Receipt" THEN {transaction.expectedreceiptdate} end
CASE WHEN {transaction.status} = 'Pending Receipt' THEN {transaction.expectedreceiptdate} end
Note that you need to use single quotes in SQL statements, and you can't specify the transaction type as part of the status condition. To work araound this, you could include the transaction type in the criteria, or add another condition within the CASE statement:
CASE WHEN {transaction.status} = 'Pending Receipt' AND {transaction.type} = 'Purchase Order' THEN {transaction.expectedreceiptdate} end

How to Correct the error in this SQL script (Database Using Azure Datawarehouse)

I am having a hard time when I try to run the script below. Error messsage:
Order by clause not valid in views and inline functions
insert into cc.s
(
id,
encid,
a_name,
a_des,
a_type,
a_value,
d_create
)
select
id,
encid,
'days_charge',
'Days 43',
'int',
(
select
datediff(day,t_dis,a.ts_it)
from
cc.enoun
where
encid <> a.encid
and id=a.pe_id
and a_source='tEst'
and a.ts_admit > t_dis
order by
tdischarge desc limit 1
) as attr_value,
getdate()
from
cc.s a
GO
In your script, you're trying to use a SELECT statement to get the a_value. Your problem here is that this select statement is your inline function. Since an inline function can't have an ORDER BY clause, you simply have to remove it.
insert into cc.s(id, encid, a_name, a_des, a_type, a_value, d_create)
select id, encid,'days_charge','Days 43','int',
(select datediff(day,t_dis,a.ts_it) from cc.enoun where encid<>a.encid and id=a.pe_id and a_source='tEst' and a.ts_admit>t_dis)
as attr_value,
getdate()
from cc.s a
GO
Also make sure that your inline statement returns only 1 value. Since you were trying to use an ORDER BY clause, that may mean that it returns multiple results.

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.

Nested IF statement returning false

I have a nested if statement is returning "False" rather than the expected outcome.
Scenario
Table "High VoltageCables" has data in it that default to numeric but may contain characters: kVa
Table "Master" checks "High VoltageCables" data as blank or not blank, and returns "Failed Check 1","Passed Check 1". This works fine.
Table "Meta" then checks the results of "Master" and then tests "High VoltageCables" data for length between 1 and 6, regardless of whether record is numeric or string.
Formula
=IF(MASTER!H2="Passed Check 1",IF(LEN('High VoltageCables'!O2)>=1,IF(LEN('High VoltageCables'!O2<6),"Passed Check 2","Failed Check 2")))
This is partially succesful, as it returns "Passed Check 2" for the following sample data in the source table "High VoltageCables".
1 numeric, or
1kVa str, or
50000 numeric
However if a field in "High VoltageCables"is blank, the formula returns "FALSE" rather than "Failed Check 1"
I inherited this task, (and would have preferred to do the whole thing in Access using relatively simple queries) - and unfortunately I am new to nested If statements, so I am probably missing something basic...
NB the data in High VoltageCables must default to numeric for a further check to work.
The first and second IF's seem to be missing the else part. They should be added at the end between the ))) like ), else ), else )
Every IF statement consists of IF( condition, truepart, falsepart) if you have two nested ifs it will be something like IF( condition, IF( condition2, truepart2, falsepart2), falsepart)
Hope that makes it a little clearer
You do have an unaccounted for FALSE in the middle IF. Try bring the latter two conditions together.
=IF(Master!H2="Passed Check 1",IF(OR(LEN('High VoltageCables'!O2)={1,2,3,4,5}),"Passed Check 2","Failed Check 2"))
It's still a bit unclear on what to show or not show if Master!H2 does not equal "Passed Check 1".
I failed to construct the formula with a concluding "else" - "Failed Check 1"
Using jeeped's and Tom's suggestion and adding the final "else" part I have solved the problem:
=IF(MASTER!H2="Passed Check 1",IF(OR(LEN('High VoltageCables'!O2)={1,2,3,4,5}),"Passed Check 2","Failed Check 2"),"Failed Check 1")

Resources