update table with multiple conditions - sql

I have table as follows:
SYS_ID SUB_NET_ID NODE_NAME NODE_ID NODE_EQ_NO NODE_VAR_NO TEMP_ID EQUIP_TYPE EQ_ID VAR_ID VAR_OBJECT VAR_NAME VAR_SUBSET VAR_SET CALC_VAR_TYPE DATA_TYPE DOF
15 1 BLEND 1 13 21 16 5 0 BLEND DEMAND SELF BLEND_OUT VAR CONTINOUS
15 1 BLEND 1 14 6 16 6 0 BLEND DEMAND BLEND BLEND VAR CONTINOUS
15 1 DEST 2 5 2 4 7 0 DEST DEMAND SELF DEST_IN VAR CONTINOUS
15 1 DEST 2 1 3 4 1 0 DEST DEMAND UNDEF DEST_IN VAR CONTINOUS
15 1 DEST 2 4 6 4 4 0 DEST MFLOW SELF DEST_IN VAR CONTINOUS
15 1 SALK 5 6 5 13 4 0 SALK MFLOW SELF SALK_OUT VAR binary
15 1 SPEN 7 8 4 13 6 0 SPEN MFLOW SELF SPEN_OUT VAR integer
I want to update the column data_type to 1 where data_type is continous and update to 0 where it is binary and so on... any suggestion

Use the CASE statement for that:
UPDATE my_tbl SET data_type =
CASE data_type
WHEN 'continous' THEN '0'
WHEN 'binary' THEN '1'
-- more options
ELSE data_type -- to retain original string if no substitute is listed
END;
You are aware that the data type will still not be a number, but what ever string type it was before, right?

Related

SQLite computed column in WHERE clause seems to be working(?)

The order in which DMBS execute queries is:
FROM -> JOIN -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT
I was surprised to find out that this query works well on SQLite:
SELECT *, duration / 60 AS length_in_hours
FROM movies
WHERE length_in_hours > 10;
Why is this query working, while in some other cases it seems to fail?
OK, So I run EXPLAIN to see what's going on here.
.eqp full
PRAGMA vdbe_trace=true;
EXPLAIN
SELECT substr(name, 1, 1) AS initial
FROM names
WHERE initial = 'D';
The query results are:
addr opcode p1 p2 p3 p4 p5 comment
---- ------------- ---- ---- ---- ------------- -- -------------
0 Init 0 11 0 0 Start at 11
1 OpenRead 0 7 0 2 0 root=7 iDb=0; names
2 Rewind 0 10 0 0
3 Column 0 1 2 0 r[2]=names.name
4 Function 6 2 1 substr(3) 0 r[1]=func(r[2..4])
5 Ne 5 9 1 80 if r[1]!=r[5] goto 9
6 Column 0 1 7 0 r[7]=names.name
7 Function 6 7 6 substr(3) 0 r[6]=func(r[7..9])
8 ResultRow 6 1 0 0 output=r[6]
9 Next 0 3 0 1
10 Halt 0 0 0 0
11 Transaction 0 0 10 0 1 usesStmtJournal=0
12 Integer 1 3 0 0 r[3]=1
13 Integer 1 4 0 0 r[4]=1
14 String8 0 5 0 D 0 r[5]='D'
15 Integer 1 8 0 0 r[8]=1
16 Integer 1 9 0 0 r[9]=1
17 Goto 0 1 0 0
In addr 0, the Init opcode sends us to addr 11 which open new Transaction.
Right after that SQLite allocate the integer 1 FOUR TIMES (addr 12-13, 15-16). That's where I started to suspect SQLite may artificially duplicate the expression before the AS into the WHERE clause.
In opcodes 3-5, which happen before the SELECT (opcodes 6-8), we can see that SQLite duplicated our substr into the WHERE clause.

Code if then statement by only using $ utility

How can I code this 'if' conditions in GAMS?
Set j/1*10/
S/1*6/;
Parameter
b(s,j) export this from excel
U(s,j) export from excel
M(s)/1 100,2 250,3 140,4 120,5 132/ export from excel
;
table b(s,j)
1 2 3 4 5 6 7 8 9 10
1 3 40 23 12 9 52 9 14 89 33
2 0 0 42 0 11 32 11 15 3 7
3 10 20 12 9 5 30 14 5 14 5
4 0 0 0 9 0 3 8 0 13 5
5 0 10 11 32 11 0 3 1 12 1
6 12 20 2 9 15 3 14 5 14 5
;
u(s,j)=0;
u(s,j)$(b(s,j))=1;
Variable delta(j); "binary"
After solving a model I got the value of delta ( suppose delta(1)=1, delta(5)=1). Then Set A is
A(j)$(delta.l(j)=1)=Yes; (A={1,5})
I want to calculate parameter R(s) according to the following :
If there is no j in A(j) s.t. j in u(s,j) then R(s)=M(s)
Else if there is a j in A(j) s.t. j in u(s,j) then R(s)=min{b(s,j): j in A(j) , j in u(s,j) }
Then R(1)=3, R(2)=11,R(3)=5, R(4)=120, R(5)=11,R(6)=12.
Is it possible to code this ' if then ' statement only by $ utility?
Thanks
Following on from the comments, I think this should work for you.
(Create a parameter that mimics your variable delta just for demonstration:)
parameter delta(j);
delta('1') = 1;
delta('5') = 1;
With loop and if/else:
Create parameter R(s). Then, looping over s , pick the minimum of b(s,A) across set A where b(s,A) is defined if the sum of b(s,A) is not zero (i.e. if one of the set is non-zero. Else, set R(s) equal to M(s).
Note, the loop is one solution to the issue you were having with mixed dimensions. And the $(b(s,A)) needs to be on the first argument of smin(.), not on the second argument.
parameter R(s);
loop(s,
if (sum(A, b(s,A)) ne 0,
R(s) = smin(A$b(s,A), b(s,A));
else
R(s) = M(s);
);
);
With $ command only (#Lutz in comments):
R(s)$(sum(A, b(s,A)) <> 0) = smin(A$b(s,A), b(s,A));
R(s)$(sum(A, b(s,A)) = 0) = M(s);
Gives:
---- 56 PARAMETER R
1 3.000, 2 11.000, 3 5.000, 4 120.000, 5 11.000, 6 12.000

SQL update column depending on other column

sequenceNo IsPoint PointNumber PointSequenceNumber IsCancel
1 1 3168 1 1
2 0 NULL 2 1
3 1 3169 2 1
4 1 2806 3 1
5 1 33322 4 1
6 1 2807 5 1
7 1 2044 6 1
8 1 2046 7 1
9 0 NULL 8 1
10 1 27524 8 1
11 1 670 9 0
12 1 671 10 0
13 1 672 11 0
14 0 NULL 12 1
15 1 1074 12 1
16 1 10844 13 0
17 1 1421 14 0
I need to insert PointNumber to other table depending on value of IsCancel Column if IsCancel is set to 1 I need to insert first iscancel and last IsCancel but sequentially
e.g. STARTPOINT => above sequenceNo 1 for point number 3168 and Iscancel set to 1 I need that point to store in other table StartPoint
ENDPOINT => above sequenceNo 15 for point number 1074 and Iscancel set to 1 I need that point to store in other table as Endpoint
TotalCANCELED => Column hold COUNT IsCancel but only if PointNumber is not NULL
I need to update these columns in other table with following numbers depending on above table
STARTPOINT ENDPOINT TOTALCANCEL
3168 1074 9
Thanks community
I find this logic a bit hard to follow. If I understand this correctly, you can use aggregation -- although you need a little twist to get the end point number:
select max(case when PointSequenceNumber = 1 then pointnumber end) as startpoint,
max(case when PointSequenceNumber = psn then pointnumber end) as endpoint,
sum(case when isCancel = 1 and PointNumber is not null end) as totalCancel
from (select t.*,
max(case when isCancel = 1 and PointNumber is not null then PointSequenceNumber end) over () as max_psn
from t
) t;

How to Interchange and update the order of rows in SQL?

I have a table called Device and now i need interchange the order of devid row for devid 5 and devid 6 .
CurrentTable
PID DEVID INID EVTYPEID EVID ALID PARMID TEXTID InputName Input2Name
1 1 0 30 0 100102 0 14 998-TCR1 998-EMG1
1 2 0 30 0 100103 0 15 998-FR 998-TCR2
9 3 0 30 0 100113 0 25 998-TCR2 998-EMG2
0 4 2 30 0 100114 0 26 998-FR NULL
8 5 18 4 53 100114 0 0 998-Sg op 998-Sg cl
4 6 17 4 53 1000114 0 0 SG_PB RA_PB
Expected Result
PID DEVID INID EVTYPEID EVID ALID PARMID TEXTID InputName Input2Name
1 1 0 30 0 100102 0 14 998-TCR1 998-EMG1
1 2 0 30 0 100103 0 15 998-FR 998-TCR2
9 3 0 30 0 100113 0 25 998-TCR2 998-EMG2
0 4 2 30 0 100114 0 26 998-FR NULL
4 6 17 4 53 1000114 0 0 SG_PB RA_PB
8 5 18 4 53 100114 0 0 998-Sg op 998-Sg cl
I do have 150 column in my table and PID and RID are Primary keys
your current select statement plus:
Order by case when DEVID = 6 then 5
when DEVID = 6 then 6
else Devid
end
Not a pretty solution but answers the question.
You need to use a temp number to switch them around.
UPDATE Device SET DEVID=-6 WHERE DEVID=6;
UPDATE Device SET DEVID=6 WHERE DEVID=5;
UPDATE Device SET DEVID=5 WHERE DEVID=-6;
If the other table has a foreign key relationship to the DEVID column then it gets a little trickier. Options:
You can break the foreign key relationship, make the switch, and then
put the key back.
You can create a temp record with DEVID=7 (or
something else not taken) and use 7 as your placeholder in the query
above (instead of -6). Don't forget in that case to delete your
dummy record when you're done.

SQL Server 2012 if one of the columns contain 1 function

I am trying to figure how I could do this where I have a table as follows:
ID FKeyID Complete
1 6 1
2 6 0
3 6 0
4 7 0
5 8 0
6 8 0
I want to create a function to return 1 or true if any FKeyID for example 6 has a value of 1 in complete column and 0 if it does not.
This is a function that takes fKey value and should return 1 or 0 based on that.
So in above basically if my FKeyID is 6 return 1 because complete column is 1 in one of the rows, and 0 for FKeyID 8 because none of values in column complete is 1.
CREATE function [dbo].f_x
(
#FKeyID int
)
RETURNS bit
as
begin
return case when exists
(select 1 from test where Complete = 1 and FKeyID = #FKeyID)
then 1 else 0 end
end