I ran the following query to retrieve data for a single investment ID:
select *, getProductClass(InstrumentID, etf_option_mapper) as ProductClass
from investor_pos
where InvestorID = '7399000270'
It returned this result:
| BrokerID | InvestorID | InstrumentID | YdPosition | Position | ProductClass | |
|---|---|---|---|---|---|---|
| 0 | 6001 | 7399000270 | 10008862 | 15 | 15 | 588000 |
I then expanded the query to include multiple investment IDs:
select *, getProductClass(InstrumentID, etf_option_mapper) as ProductClass
from investor_pos
where InvestorID in ('7399000270', '00130239')
Now I see this result:
| BrokerID | InvestorID | InstrumentID | YdPosition | Position | ProductClass | |
|---|---|---|---|---|---|---|
| 0 | 0089 | 00130239 | IF2509 | 3 | 3 | IF |
| 1 | 0089 | 00130239 | IM2509 | -2 | -2 | IM |
| 2 | 0089 | 00130239 | IM2509 | 2 | 2 | IM |
| 3 | 0089 | 00130239 | IF2509 | -3 | -3 | IF |
| 4 | 6001 | 7399000270 | 10008862 | 15 | 15 |
Note the ProductClass value is now NULL on the last line. However, when I query for the single investment code, this specific data row returns a non-NULL result (corresponding to the above 588000 value).
Why would this produce different results? Could it be due to limitations of iif function when handling vectors versus scalars?
The complete reproducible script is provided below:
investor_pos.csv
| BrokerID | InvestorID | InstrumentID | YdPosition | Position |
|---|---|---|---|---|
| 0089 | 00130239 | IF2509 | 3 | 3 |
| 0089 | 00130239 | IM2509 | -2 | -2 |
| 0089 | 00130239 | IM2509 | 2 | 2 |
| 0089 | 00130239 | IF2509 | -3 | -3 |
| 6001 | 7399000270 | 10008862 | 15 | 15 |
etf_option_mapper
etf_option_mapper = dict("STRING", ANY)
etf_option_mapper["10008862"] = [588000]
scripts
investor_pos = loadText("<YourPath>/nvestor_pos.csv")
def getProductClass(InstrumentID,etf_option_mapper){
productClass = left(InstrumentID,4).regexReplace("[0-9]+","").regexReplace('HO','IH').regexReplace('IO','IF').regexReplace('MO','IM')
return iif(isDigit(InstrumentID),etf_option_mapper[InstrumentID][0],productClass)
}
select *,getProductClass(InstrumentID,etf_option_mapper) as ProductClass from investor_pos where InvestorID='7399000270'
select *,getProductClass(InstrumentID,etf_option_mapper) as ProductClass from investor_pos where InvestorID in ('7399000270','00130239')
getProductClass(InstrumentID, etf_option_mapper)result numeric or string? ('IF','IM', 588000). Also try run query `` ... where InvestorID in ('7399000270', '00130239')`` withorder by InvestorId descandorder by InvestorId asc.