Is there anyway to store an expression in an IronPython script in Spotfire, and then call on it later to simplify my scripts where I am adding a bunch of calculated columns?
Some of my expressions are simply (A+B), but i am also trying to see if it is possible to store an if then "case statement" as an expression
For example:
expression1 = [VarA] * 3.289
expression2 ~ (VarA] / [VarB]) * 23.33
expression3 = Case
When [VarA] = 1 Then "Blue"
Else "Red"
End
example script (this works):
from Spotfire.Dxp.Data import CalculatedColumn
cols = Document.Data.Tables["MyTable"].Columns
cols.AddCalculatedColumn("NewColumn1","[VarA] * 3.289");
Example of what I'd like to do:
from Spotfire.Dxp.Data import CalculatedColumn
cols = Document.Data.Tables["MyTable"].Columns
cols.AddCalculatedColumn("NewColumn1","expression1");
cols.AddCalculatedColumn("NewColumn2","expression2");
cols.AddCalculatedColumn("NewColumn3","expression3");
I mean maybe I'm not understanding you but I think just make expression1 a variable like you were trying to do. Make sure you use quotation marks for strings and don't use them for variables.
expression1 = "[varA]*3.289"
cols.AddCalculatedColumn("NewColumn1",expression1)
Related
I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.
I'm working with a query that is used by multiple services but the number of results returned are different based on filtering.
To avoid copying and pasting the query, I was wondering if it was possible to pass in piece of sql into a sql parameter and it would work? I'm also open to alternative solutions.
EXAMPLE:
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("filter", "and color = blue");
namedParameterJdbcTemplate.query(“select * from foo where name = 'Joe' :filter”, parameters, new urobjRowMapper());
It is very dangerous and fragile to let callers pass SQL to your program, because it opens you up to SQL injection - the very problem the parameters are there to prevent.
A better approach is to pre-code the filters in your query, and protect them by a special "selector" parameter:
SELECT *
FROM foo
WHERE name='Joe' AND
(
(:qselect = 1 AND color='blue')
OR (:qselect = 2 AND startYear = 2021)
OR (:qselect = 3 AND ...)
)
I'm generating SQL programmatically so that, based on certain parameters, the query that needs to be executed could be different (i.e., tables used, unions, etc). How can I insert a string like this: "select * from table", into a %%sql block? I know that using :variable inserts variable into the %%sql block, but it does so as a string, rather than sql code.
The answer was staring me in the face:
query="""
select
*
from
sometable
"""
%sql $query
If you want to templatize your queries, you can use string.Template:
from string import Template
template = Template("""
SELECT *
FROM my_data
LIMIT $limit
""")
limit_one = template.substitute(limit=1)
limit_two = template.substitute(limit=2)
%sql $limit_one
Source: JupySQL documentation.
Important: If you use this approach, ensure you trust/sanitize the input!
I'm trying to take the following sql case statement and convert it to Access 2010 as a calculation for a column. I've looked at the IIF statements but have received errors trying to sort it out. Thank you for any help.
case
when left(Tiers,4) = 'Tier' and isnumeric(right((left(Tiers,7)),2)) = 1 then right((left(Tiers,7)),2)
when left(Tiers,4) = 'Tier' and isnumeric(right((left(Tiers,7)),2)) = 0 then right((left(Tiers,6)),1)
else Tiers
end
;
you can only use Case statements in access in VBA Code.
and iif should work as well in access
like this
iif(left(Tiers,4) ='Tire',iif(isnumeric(right((left(Tiers,7)),2)) = 1,right((left(Tiers,7)),2),right((left(Tiers,6)),1)), Tires)
You can use IIF() as you mentioned and it will likely be a pain. There is also SWITCH function available which will likely make it easier for you to convert your case statement instead of nested IIF()s
Switch( expr-1, value-1 [, expr-2, value-2 ] … [, expr-n, value-n ] )
https://support.office.com/en-us/article/Switch-Function-d750c10d-0c8e-444c-9e63-f47504f9e379
This is a duplicate question of:
Case expressions in Access.
I'm looking for a concise one line method to get a single value from a DataTable using lambda using VB.Net
This code works
OPT_UDLY = (From X In DATA.OPTIONs
Where X.CONTRACT = CTC
Select X.UDLY).Single()
but I'd like to get the value in one line like this (which doesn't work)
OPT_UDLY = DATA.Where(function(t) t.contract.Equals(CTC)).Distinct()
Any suggestions please?
Try this:
OPT_UDLY = Data.OPTIONs.Where(Function(t) t.CONTRACT = CTC).Select(Function(x) x.UDLY).Single()
Since you use Data.OPTIONs in the multiple LINQ lines, it should be consistently used for single line too.
Also, since you only need the UDLY element, use further Select LINQ expression in single line style, like what you did in your multiple lines.
And lastly, use Single to return the matching value if there is only one match, like what you already did.
Just remove the line feeds:
OPT_UDLY = (From X In DATA.OPTIONs Where X.CONTRACT = CTC Select X.UDLY).Single()
It's shorter and more readable than the lambda equivalent:
OPT_UDLY = Data.OPTIONs.Where(Function(t) t.CONTRACT = CTC).Select(Function(x) x.UDLY).Single()