How can I get a specific element of a set in GAMS - gams-math

Suppose we have the following code:
sets
i index of products /product_1, product_2/;
parameters
Inventory_Cost(i) /product_1 3, product_2 4/;
Now when I want to use Inventory_Cost(3-i) it gives me an error. Inventory_Cost(3-ord(i)) doesn't work also. How should i correct this?

Not sure, what you want to do with this, but it should work like this:
sets
i index of products /product_1, product_2/;
parameters
Inventory_Cost(i) /product_1 3, product_2 4/
switchI(i);
switchI(i) = Inventory_Cost(i+[card(i)-2*ord(i)+1]);
display switchI;

Related

Microsoft SQL Server generates two select queries and puts data in separate columns

I am looking for a query that separate the data with the condition WHERE in the same output but in separates columns.
Example: I have the table Product_2:
I have two separates queries (to separate the products by Produt_Tag):
SELECT
Product_Mark AS "PIT-10_Product_Mark",
Product_Model AS "PIT-10_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-10';
SELECT
Product_Mark AS "PIT-11_Product_Mark",
Product_Model AS "PIT-11_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-11';
And I get this output:
But I need the output to be like this:
Can someone tell me how I need to modify my query to have the four columns in the same table/ output?
Thank you
I forgot to tell that in the data I Have the “Porduct_Mark” that only appears one time. (in reality the data in “Product_Mark” is the name of the place where the instrument is located and one place can have one or two instruments “Product_Model”. At the end I’m looking for the result show in the image here below. I tried to use LEFT JOIN but that don’t work.
here is the new table "Product_2"
Result that I'm looking for:
Luis Ardila
I am assuming Product_PK is the primary key for the table and the repeated value 1002 shown in the question is a mistake. Considering this assumption, you can get the result set using self join as below.
SELECT pa.Product_Mark AS "PIT-10_Product_Mark", pa.Product_Model AS "PIT-10_Product_Model",
pb.Product_Mark AS "PIT-11_Product_Mark", pb.Product_Model AS "PIT-11_Product_Model"
FROM Product_2 pa
INNER JOIN Product_2 pb
ON pa.Product_Mark = pb.Product_Mark
WHERE pa.product_pk != pb.product_pk
and pa.Product_Tag = 'PIT-10'
and pb.Product_Tag = 'PIT-11';
verified same in https://dbfiddle.uk/NiOO8zc1

SQL Server 2008 AND/OR operators and parentheses

I am having a bit of trouble with AND/OR operators. It's easy stuff, but I'm having trouble with the following scenario:
WHERE (ID = '111') OR (charge_desc LIKE '%garn%') OR
(charge_desc LIKE '%levy%') OR
(charge_desc LIKE '%exe%')
Basically, I am trying to find the keywords based only on ID 111. However, what's happening with the current query is that it looks for those keywords based on a bunch of different IDs instead. I've tried to manipulate the parenthesis, and I see the issue. Because it can't be "111 OR LIKE XYZ..." but it can't be AND either because if you do AND, then it looks for that ID based on EXACTLY those 3 criteria.
I am looking to do ID 111 and look for garn or levy or exe - not "AND" but it should be every variation that comes up but ONLY for 111.
I hope this makes sense. I feel its something with the parenthesis.
I just tried this:
WHERE ID = 111
HAVING garn or levy or exe
Forgive my lack of syntax there. :)
If you absolutely must have id = 111 then this is an and relationship, not an or relationship. You can then have a series of or operators between the other conditions:
WHERE ID = '111' AND
(charge_desc LIKE '%garn%' OR
charge_desc LIKE '%levy%' OR
charge_desc LIKE '%exe%')
The problem is with the parenthesis.
Let's look at this scenario:
WHERE (ID = '111') OR (charge_desc LIKE '%garn%') OR
(charge_desc LIKE '%levy%') OR
(charge_desc LIKE '%exe%')
You are saying where ID = 111 OR Charg_Desc like garn...etc.
WHERE (ID = '111') AND
(charge_desc LIKE '%garn%' OR
charge_desc LIKE '%levy%' OR
charge_desc LIKE '%exe%')
Now, you're saying WHERE ID = 111 AND the following conditional OR is applied to bring in the additional match. Remember, the match is in addition to the ID match.

SQL statement that returns sums of records and returns both make names in SQL Server

I'm a big confused on this one. I'm trying to use a SQL statement that sums up two different records but displays both 'Makes' of each row. I currently have this statement..
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
FROM
tObjective, tMake, tDealerships
WHERE
tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%' OR tMake.Make LIKE '%GMC%')
GROUP BY
tDealerships.CapShortName
which returns this..
However, I need to display the two makes that it is summing up which is Buick and GMC.
If I add the make in the statement, I must group by the make which then separates them into two row sums.. one for Buick and then one for GMC. Is there a better way or doing this? I am able to make it do what I need it to do? I have been stuck on this one for a bit now. Any suggestions/help is greatly appreciated!
EDIT: The ideal result is having 1 result with an additional column named Make that displays both Buick/GMC together.
You had to hard-code the makes for the LIKE strings. Why not just hard code them in a literal for the make column?
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
------
'GMC/Buick' As Make
------
FROM tObjective, tMake, tDealerships
WHERE tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%'
OR tMake.Make LIKE '%GMC%')
GROUP BY tDealerships.CapShortName
I think that something as simple as grouping on the make as well would solve your problem, if I am reading it correctly?
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
FROM tObjective, tMake, tDealerships
WHERE tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%'
OR tMake.Make LIKE '%GMC%')
GROUP BY tMake.Make, tDealerships.CapShortName

Trying to sort result obtained from Entity Framework

Step 1: I am using entity to get the result. I run a query like this
Dim inbox = From p In dbContext.Inboxes Where p.RecordId = member_id Select p
Step 2: After that I put that into
Dim inboxList As IEnumerable(Of Entities.Inbox) = inbox.ToList()
so somewhere between step 1 and 2 I need to sort the List like so
inbox.OrderByDescending(Function(p) p.Importance) <-- PROBLEM HERE
The above line seems to have results, no exceptions, it just does not do anything!?
Any advice?
Order by will return a re-arranged value it won't actually rearrange itself.
You want:
inbox = inbox.OrderByDescending(Function(p) p.Importance)
Without assigning an output to a variable, it won't do anything

Coldfusion: SELECT FROM QoQ WHERE X in (SELECT Y FROM QoQ) not working

In CF I am trying to do a QoQ where the rows are in a list of other rows. Basically moving some code from cftags to cfscript (Not important why). In tags we have a main query and we have several nests that do some heavy lifting. I am moving this to cfscript and have the following syntax that is working:
var submissionList = new Query(dbtype="query", QoQsrcTable=ARGUMENTS.answers, sql="
SELECT submission_id FROM QoQsrcTable GROUP BY submission_id
").execute().getResult();
var submissions = new Query(dbtype="query", QoQsrcTable=ARGUMENTS.answers, sql="
SELECT * FROM QoQsrcTable WHERE submission_id IN (#submissionList.submission_id#)
").execute().getResult();
I have tried the following but it fails to work:
var submissions = new Query(dbtype="query", QoQsrcTable=ARGUMENTS.answers, sql="
SELECT * FROM QoQsrcTable WHERE submission_id IN (SELECT submission_id FROM QoQsrcTable GROUP BY submission_id)
").execute().getResult();
I think the second example should work. I've tried messing with it in various ways. But can't seem to figure out what I am doing wrong. Maybe a nested QoQ doesn't work like that. Is there another way I can accomplish what I am trying without two chunks of code? Just so it's more readable and I don't have to assign variables twice.
QoQ doesn't support subqueries. That's the long and the short of it.
Docs
In Coldfusion 10 or Railo 4, you can utilize the groupBy function of Underscore.cfc to accomplish what you want in much less code:
_ = new Underscore();// instantiate the library
submissions = _.groupBy(arguments.answers, 'submission_id');
groupBy() returns a structure where the keys are the values of the group element (in this case, submission_id).
(Disclaimer: I wrote Underscore.cfc)