Excluding an element from a predefined set in GAMS ? - gams-math

Over a predefined set S /s1*s100/ I need to write something like this
M_s - M_shat =g= 0 , where (shat) is any other element in the set (S) except the element (s).
How can I do that in GAMS?
Thanks.

You could use an alias set and a conditional with the SameAs operator.
Alias(S,SS);
E_M(s,ss)$(not SameAs(s,ss)).. M(s) - M(ss) =G= 0;

Related

Logic for adding \ before every character in ADF parameter

I have a requirement in ADF wherein i have a parameter which can contain following values AB or A or B
I want to update these values by appending \ in front of each character.
So the output should be as follows:
For AB - \A\B
For A - \A
How to append \ in front of every character irrespective of number of characters in a parameter?
Please note that we don't want to use DataFlow for this.
You cannot do this with the native expressions in ADF. Either call an Azure function to perform the work, or use SQL and a Lookup Activity to perform the task. You can put the following into a dynamic expression referencing the following as an example;
declare ##str nvarchar(max) = 'helloworld'; /* get value in dynamic expression '#{#pipeline().parameters.PARAMETER_NAME}'*/
declare ##strOut nvarchar(max) = '';
declare ##strlen int = len(##str);
declare ##i int = 1;
while ##i <= ##strlen
begin
set ##strOut += concat('\',SUBSTRING(##str,##i,1));
set ##i +=1;
end
select ##strOut [result];
Then fetch the result from the activity. e.g. #activity('Lookup1').output.firstRow.result
As seen below, note the UI is just adding the extra \ in the preview, (ADF rendering bug)
The dynamic expression in the Lookup Query parameter :

Filter using string with any possible content in BigQuery

I am a newbie using BigQuery.
I am building a query that I will share with several other people. Each person is responsible for different business units and I want them to be able to easily insert the name of their business units in this query.
I built something like this, and it works fine from what I tested:
DECLARE business_units array<string>;
SET business_units = ["unit_A", "unit_C", "unit_D"];
SELECT *
FROM dataset
WHERE bu_name IN UNNEST(business_units)
Problem
I also want to be able to easily change that query in order to search for all possible business units.
Ideally, I just want to change the "SET" line. I tried different things but none of them seem to work. I believe that I need to use metacharacters or regular expression, but I am not being able to find the right combination. I have already looked into the BigQuery documentation, but I am not being able to understand how to do this.
I have tried things like:
SET business_units = ["."];
SET business_units = ["*"];
SET business_units = ["\."];
SET business_units = ["%%"];
When I use any of these, my result return as empty.
Could someone point me in the right direction, please?
There are many options for you here. I will show you those with minimal changes to your original solution
Option #1
DECLARE business_units array<string>;
SET business_units = ["unit_A", "unit_C", "unit_D", "ALL_UNITS"];
SELECT *
FROM dataset
WHERE bu_name IN UNNEST(business_units)
OR "ALL_UNITS" IN UNNEST(business_units);
As you can see here - when you want all units - add "ALL_UNITS" in your SET line
Option #2
DECLARE business_units array<string>;
DECLARE all_units boolean;
SET business_units = ["unit_A", "unit_C", "unit_D"];
SET all_units = TRUE;
SELECT *
FROM dataset
WHERE bu_name IN UNNEST(business_units)
OR all_units;
here - you have one more parameter all_units. When you want to see all units - just set it to TRUE, otherwise to FALSE
IN can not process a list using LIKE or regular expressions, and LIKE or regular expressions can't take arrays as parameters.
The straight forward approach is to just use a JOIN on you un-nested list.
DECLARE business_units array<string>;
SET business_units = ["unit_A", "unit_C", "unit_D"];
SELECT
*
FROM
dataset
INNER JOIN
UNNEST(business_units) AS param_pattern
ON dataset.buname LIKE param_pattern
If a row matches more than one element in the array, you'll get duplication (each dataset row joined with every pattern that it matches).
How you deal with that is up to you. You might just have SELECT DISTINCT dataset.*, but your question doesn't cover that. (If you're unsure how to proceed with that, open another question once you have this part working.)

How to write a while loop in GAMS

Can anyone help me with this problem please. I want to add to i one by one and put the amount of x(i) equal to 1 in each step, so i wrote it as below but it's not working
loop(i,
x('0')=1;
t('0')=1;
while(t>m,
ord(i)=ord(i)+1;
display i;
x(i)=1;
display x;
t(i)=t(i-1) +1;
);
);
And by the way m is a variable which is calculated before this, in an equation.
If m is a variable from a model that was solved before, you should check its level using the .l attribute:
...
while(t>m.l,
...
Look at this page to have a better idea of how to make a while loop in GAMS. Also look at this code because it may help you:
root=minroot;
*find a sign switch
while(signswitch=0 and root le maxroot,
root=root+inc;
function_value2= a-b*root+c*sqr(root);
if((sign(function_value1) ne sign(function_value2)
and abs(function_value1) gt 0
and abs(function_value2) gt tolerance),
maxroot=root;
signswitch=1
else
if(abs(function_value2) gt tolerance,
function_value1=function_value2;
minroot=root;);
);
* display 'inside',minroot,maxroot,function_value1,function_value2;
);

How to remove unnecessary line breaks in SQL Plus Spooling?

I am spooling a package from a database and this is what I get:
CREATE OR REPLACE PACKAGE BODY "CPI"."GIPI_WBOND_BASIC_PKG"
AS
FUNCTION get_gipi_wbond_basic (p_par_id gipi_wbond_basic.par_id%TYPE)
RETURN gipi_wbond_basic_tab PIPELINED
IS
v_wbond gipi_wbond_basic_type;
BEGIN
FOR i IN (SELECT a.par_id, a.obligee_no, a.bond_dtl, a.inde
mnity_text,
a.clause_type, a.waiver_limit, a.contract_date, a.cont
ract_dtl,
a.prin_id, a.co_prin_sw, a.np_no, a.coll
_flag,
a.plaintiff_dtl, a.defendant_dtl, a.civil_case_no
FROM gipi_wbond_basic a
WHERE a.par_id = p_par_id)
And I am expecting it to be something like this:
CREATE OR REPLACE PACKAGE BODY cpi.gipi_wbond_basic_pkg
AS
FUNCTION get_gipi_wbond_basic (p_par_id gipi_wbond_basic.par_id%TYPE)
RETURN gipi_wbond_basic_tab PIPELINED
IS
v_wbond gipi_wbond_basic_type;
BEGIN
FOR i IN (SELECT a.par_id, a.obligee_no, a.bond_dtl, a.indemnity_text,
a.clause_type, a.waiver_limit, a.contract_date,
a.contract_dtl, a.prin_id, a.co_prin_sw, a.np_no,
a.coll_flag, a.plaintiff_dtl, a.defendant_dtl,
a.civil_case_no
FROM gipi_wbond_basic a
WHERE a.par_id = p_par_id)
Please help me on how can I get rid of those new lines and ugly format. Thanks!
Ok this one solved my problem.
From this,
SET HEADING OFF;
SET ECHO OFF;
SET PAGES 999;
SET LONG 999999;
I added this:
SET LONGCHUNKSIZE 999999;
SET PAGESIZE 0;
SET LINESIZE 500;
TO remove extra Line breaks Try :-
SET FEED OFF
Additionally (to ladiesman1792's own answers, which were below this post when I posted!):
column text format a120 --<< will wrap at 120 chars (maybe bytes, not sure on that)
This is an Oracle sqlplus directive, as per the other answers.
You can do it using the regular expression in SSMS:
1) Ctrl-H to bring up the Find And Replace window
2) Select USE -> Regular Expressions
3) Put ^\n in the Find What
4) Keep Replace With empty
5) Click Replace (All)
Good luck
-- Nilesh Umaretiya (India)

Using comma with an update sentence

I want to update a row which has some html tags inside. For instance:
src='/imagem.png'></ p></ body>
> UPDATE ISTANBUL_TABLE SET TEXT = '<
> body>< p>< img src='/imagem.png '></
> p></ body>' WHERE 1=1
You see after src=' means the query ends, but it does not end. How can i solve it without using " (double comma)? Any solution please?
best regards bk
You need to escape the single-quotes, by typing them twice:
UPDATE ISTANBUL_TABLE SET TEXT = '< body>< p>< img src=''/imagem.png ''>' WHERE 1=1
Also, your WHERE clause is nonsensical and can be dropped entirely
UPDATE ISTANBUL_TABLE SET TEXT = '<body><p><img src=''/imagem.png''>'
Use parameterised SQL:
UPDATE ISTANBUL_TABLE SET TEXT = #HTML WHERE...
Then from your calling code, you just pass in the #HTML parameter and don't need to double up the single quotes.