Find and insert a value from previous row based on a match between dimensions into the same SQL database table - sql
Is it possible to get 'Dimension 3' values from Group 'Extension' into rows where Group is 'Finishing' and 'Primer'?
The problem is that the program sending the data into SQL does not give 'Dimension3' values for Groups 'Finishing' and 'Primer'.
Database table [3E_IDB_Aru].dbo.Material structure looks like this:
ID
P_GUID
Area
Group
Code
Variant
Dimension1
Dimension2
Dimension3
54
519AEC
0,0504
Extension
27/1 ver
sj
2520
20
110
55
519AEC
0,0504
Finishing
RAL 9005
(kein)
2520
20
0
56
519AEC
0,0504
Finishing
RAL 9010
(kein)
2520
20
0
57
519AEC
0,1008
Primer
GW201
(kein)
2520
40
0
58
519AEC
0,1008
Primer
ZW-400
(kein)
2520
40
0
59
519AEC
0,0504
Extension
27/1 ver
sj
2520
20
50
60
519AEC
0,0504
Finishing
RAL 9005
(kein)
2520
20
0
61
519AEC
0,0504
Finishing
RAL 9010
(kein)
2520
20
0
62
519AEC
0,1008
Primer
GW201
(kein)
2520
40
0
63
519AEC
0,1008
Primer
ZW-400
(kein)
2520
40
0
64
519AEC
0,0296
Extension
27/1 top
sj
1480
20
110
65
519AEC
0,0296
Finishing
RAL 9005
(kein)
1480
20
0
66
519AEC
0,0296
Finishing
RAL 9010
(kein)
1480
20
0
67
519AEC
0,0592
Primer
GW201
(kein)
1480
40
0
68
519AEC
0,0592
Primer
ZW-400
(kein)
1480
40
0
The desired result should look like this (values must be written into the same table [3E_IDB_Aru].dbo.Material):
ID
P_GUID
Area
Group
Code
Variant
Dimension1
Dimension2
Dimension3
54
519AEC
0,0504
Extension
27/1 ver
sj
2520
20
110
55
519AEC
0,0504
Finishing
RAL 9005
(kein)
2520
20
110
56
519AEC
0,0504
Finishing
RAL 9010
(kein)
2520
20
110
57
519AEC
0,1008
Primer
GW201
(kein)
2520
40
110
58
519AEC
0,1008
Primer
ZW-400
(kein)
2520
40
110
59
519AEC
0,0504
Extension
27/1 ver
sj
2520
20
50
60
519AEC
0,0504
Finishing
RAL 9005
(kein)
2520
20
50
61
519AEC
0,0504
Finishing
RAL 9010
(kein)
2520
20
50
62
519AEC
0,1008
Primer
GW201
(kein)
2520
40
50
63
519AEC
0,1008
Primer
ZW-400
(kein)
2520
40
50
64
519AEC
0,0296
Extension
27/1 top
sj
1480
20
110
65
519AEC
0,0296
Finishing
RAL 9005
(kein)
1480
20
110
66
519AEC
0,0296
Finishing
RAL 9010
(kein)
1480
20
110
67
519AEC
0,0592
Primer
GW201
(kein)
1480
40
110
68
519AEC
0,0592
Primer
ZW-400
(kein)
1480
40
110
You can use an UPDATE for this.
UPDATE Material
SET Dim3 = Dimension3
WHERE (MaterialGroup = 'Finishing' OR MaterialGroup = 'Primer')
AND Dim3 IS NOT NULL
But you may want to leave your data unchanged and do this operation in your SELECT operations instead. This is especially true if you have a great many rows already.
SELECT ID, Fieldnumber, Area, MaterialGroup, Code, Variant,
Dimension1, Dimension2, Dimension3,
CASE WHEN Dim3 IS NOT NULL THEN Dim3
WHEN MaterialGroup = 'Finishing' THEN Dimension3
WHEN MaterialGroup = 'Primer' THEN Dimension3
ELSE Dim3
END AS Dim3
You might even create a view for this purpose if that makes sense in your application.
use a cross apply between the source and target datasets. the source data set uses a case when condition to find group equal to Primer and Finishing to move dimension3 into the dim3 column
declare #tmp as table(ID int, P_GUID varchar(20), Area varchar(20), [Group] varchar(20), Code varchar(20), Variant varchar(20), Dimension1 int, Dimension2 int, Dimension3 int,Dim3 int)
INSERT INTO #tmp(ID,P_GUID, Area, [Group], Code, Variant, Dimension1, Dimension2, Dimension3, Dim3)
values
(54,'519AEC','0,0504','Extension','27/1 ver','sj',2520,20,110,NULL)
,(55,'519AEC','0,0504','Finishing','RAL 9005','(kein)',2520,20,0,NULL)
,(56,'519AEC','0,0504','Finishing','RAL 9010','(kein)',2520,20,0,NULL)
,(57,'519AEC','0,1008','Primer','GW201','(kein)',2520,40,0,NULL)
,(58,'519AEC','0,1008','Primer','ZW-400','(kein)',2520,40,0,NULL)
,(59,'519AEC','0,0504','Extension','27/1 ver','sj',2520,20,110,NULL)
,(60,'519AEC','0,0504','Finishing','RAL 9005','(kein)',2520,20,0,NULL)
,(61,'519AEC','0,0504','Finishing','RAL 9010','(kein)',2520,20,0,NULL)
,(62,'519AEC','0,1008','Primer','GW201','(kein)',2520,40,0,NULL)
,(63,'519AEC','0,1008','Primer','ZW-400','(kein)',2520,40,0,NULL)
,(64,'519AEC','0,0296','Extension','27/1 top','sj',1480,20,110,NULL)
,(65,'519AEC','0,0296','Finishing','RAL 9005','(kein)',1480,20,0,NULL)
,(66,'519AEC','0,0296','Finishing','RAL 9010','(kein)',1480,20,0,NULL)
,(67,'519AEC','0,0592','Primer','GW201','(kein)',1480,40,0,NULL)
,(68,'519AEC','0,0592','Primer','ZW-400','(kein)',1480,40,0,NULL)
update target
set
target.Dim3=source.Dim3
from #tmp as target
cross apply
(
select
case when [Group] in('Finishing', 'Primer') then Dimension3 end Dim3
from #tmp data
where target.ID=data.ID
)source
select * from #tmp
output
ID P_GUID Area Group Code Variant Dimension1 Dimension2 Dimension3 Dim3
54 519AEC 0,0504 Extension 27/1 ver sj 2520 20 110 NULL
55 519AEC 0,0504 Finishing RAL 9005 (kein) 2520 20 0 0
56 519AEC 0,0504 Finishing RAL 9010 (kein) 2520 20 0 0
57 519AEC 0,1008 Primer GW201 (kein) 2520 40 0 0
58 519AEC 0,1008 Primer ZW-400 (kein) 2520 40 0 0
59 519AEC 0,0504 Extension 27/1 ver sj 2520 20 110 NULL
60 519AEC 0,0504 Finishing RAL 9005 (kein) 2520 20 0 0
61 519AEC 0,0504 Finishing RAL 9010 (kein) 2520 20 0 0
62 519AEC 0,1008 Primer GW201 (kein) 2520 40 0 0
63 519AEC 0,1008 Primer ZW-400 (kein) 2520 40 0 0
64 519AEC 0,0296 Extension 27/1 top sj 1480 20 110 NULL
65 519AEC 0,0296 Finishing RAL 9005 (kein) 1480 20 0 0
66 519AEC 0,0296 Finishing RAL 9010 (kein) 1480 20 0 0
67 519AEC 0,0592 Primer GW201 (kein) 1480 40 0 0
68 519AEC 0,0592 Primer ZW-400 (kein) 1480 40 0 0
(NOTE: the code is run twice, because MaterialGroup 'Primer' Dimension2 value is double the value of MaterialGroup 'Extension' Dimension2 value. On the first pass 'null' values are being written into 'Primer' Dimension3 rows. The second pass will take care of this as I did not know how to nest this, if possible at all, into the first part of the code)Here is the code I used to achieve the task:
PRINT '\tAdd Dimension3 values into Material table where Dimension3 = 0'
UPDATE t
SET Dimension3 = (
SELECT Dimension3 FROM Material
WHERE
Pos_GUID = t.Pos_GUID AND Dimension1 = t.Dimension1 AND Dimension2 = t.Dimension2 AND ID = (
SELECT MAX(ID) FROM Material
WHERE
Pos_GUID = t.Pos_GUID AND Dimension1 = t.Dimension1 AND Dimension2 = t.Dimension2 AND ID < t.ID AND Dimension3 > 0 )
)
FROM Material t
WHERE t.Dimension3 = 0
PRINT '\tAdd Dimension3 values into Material table where Dimension3 = null'
UPDATE t
SET Dimension3 = (
SELECT Dimension3 FROM Material
WHERE
Pos_GUID = t.Pos_GUID AND Dimension1 = t.Dimension1 AND Dimension2 * 2 = t.Dimension2 AND ID = (
SELECT MAX(ID) FROM Material
WHERE
Pos_GUID = t.Pos_GUID AND Dimension1 = t.Dimension1 AND Dimension2 * 2 = t.Dimension2 AND ID < t.ID AND Dimension3 > 0 )
)
FROM Material t
WHERE t.Dimension3 is NULL
See the db<>fiddle here
Below is an example of the real database with some unnecessary columns removed. Here the Pos_GUID values are all mixed, but the ID numbers are always in ascending order under the same Pos_GUID:
ID
Pos_GUID
MaterialGroup
Dimension1
Dimension2
Dimension3
149
2E31E29F763844DA806B9868C403E219
Finishing
1361
19.5
0
149
519AEC1C7EB54C0B8DBC562A5194E38A
Extension
1500
18
89.5
150
2E31E29F763844DA806B9868C403E219
Primer
1361
19.5
0
150
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
1500
18
0
151
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
1500
18
0
152
2E31E29F763844DA806B9868C403E219
Finishing
551.67
19.5
0
152
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
1500
36
0
153
2E31E29F763844DA806B9868C403E219
Primer
551.67
19.5
0
153
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
1500
36
0
154
519AEC1C7EB54C0B8DBC562A5194E38A
Extension
2520
20
50
155
2E31E29F763844DA806B9868C403E219
Finishing
1361
19.5
0
155
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
0
156
2E31E29F763844DA806B9868C403E219
Primer
1361
19.5
0
156
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
0
157
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
2520
40
0
158
2E31E29F763844DA806B9868C403E219
Finishing
2000
82
0
158
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
2520
40
0
159
2E31E29F763844DA806B9868C403E219
Finishing
2000
82
0
159
519AEC1C7EB54C0B8DBC562A5194E38A
Extension
2520
20
110
160
2E31E29F763844DA806B9868C403E219
Primer
2000
164
0
160
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
0
161
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
0
162
2E31E29F763844DA806B9868C403E219
Finishing
2000
82
0
162
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
2520
40
0
Result:
ID
Pos_GUID
MaterialGroup
Dimension1
Dimension2
Dimension3
149
2E31E29F763844DA806B9868C403E219
Finishing
1361
19.5
0
149
519AEC1C7EB54C0B8DBC562A5194E38A
Extension
1500
18
89.5
150
2E31E29F763844DA806B9868C403E219
Primer
1361
19.5
0
150
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
1500
18
89.5
151
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
1500
18
89.5
152
2E31E29F763844DA806B9868C403E219
Finishing
551.67
19.5
0
152
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
1500
36
89.5
153
2E31E29F763844DA806B9868C403E219
Primer
551.67
19.5
0
153
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
1500
36
89.5
154
519AEC1C7EB54C0B8DBC562A5194E38A
Extension
2520
20
50
155
2E31E29F763844DA806B9868C403E219
Finishing
1361
19.5
0
155
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
50
156
2E31E29F763844DA806B9868C403E219
Primer
1361
19.5
0
156
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
50
157
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
2520
40
50
158
2E31E29F763844DA806B9868C403E219
Finishing
2000
82
0
158
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
2520
40
50
159
2E31E29F763844DA806B9868C403E219
Finishing
2000
82
0
159
519AEC1C7EB54C0B8DBC562A5194E38A
Extension
2520
20
110
160
2E31E29F763844DA806B9868C403E219
Primer
2000
164
0
160
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
110
161
519AEC1C7EB54C0B8DBC562A5194E38A
Finishing
2520
20
110
162
2E31E29F763844DA806B9868C403E219
Finishing
2000
82
0
162
519AEC1C7EB54C0B8DBC562A5194E38A
Primer
2520
40
110
See also this db<>fiddle with the actual database logic here
Related
pandas df add new column based on proportion of two other columns from another dataframe
I have df1 which has three columns (loadgroup, cartons, blocks) like this loadgroup cartons blocks cartonsPercent blocksPercent 1 2269 14 26% 21% 2 1168 13 13% 19% 3 937 8 11% 12% 4 2753 24 31% 35% 5 1686 9 19% 13% total(sum of column) 8813 68 100% 100% The interpretation is like this: out of df1 26% cartons which is also 21% of blocks are assigned to loadgroup 1, etc. we can assume blocks are 1 to 68, cartons are 1 to 8813. I also have df2 which also has cartons and blocks columns. but does not have loadgroup. My goal is to assign loadgroup (1-5 as well) to df2 (100 blocks 29608 cartons in total), but keep the proportions, for example, for df2, 26% cartons 21% blocks assign loadgroup 1, 13% cartons 19% blocks assign loadgroup 2, etc. df2 is like this: block cartons 0 533 1 257 2 96 3 104 4 130 5 71 6 68 7 87 8 99 9 51 10 291 11 119 12 274 13 316 14 87 15 149 16 120 17 222 18 100 19 148 20 192 21 188 22 293 23 120 24 224 25 449 26 385 27 395 28 418 29 423 30 244 31 327 32 337 33 249 34 528 35 528 36 494 37 540 38 368 39 533 40 614 41 462 42 350 43 618 44 463 45 552 46 397 47 401 48 397 49 365 50 475 51 379 52 541 53 488 54 383 55 354 56 760 57 327 58 211 59 356 60 552 61 401 62 320 63 368 64 311 65 421 66 458 67 278 68 504 69 385 70 242 71 413 72 246 73 465 74 386 75 231 76 154 77 294 78 275 79 169 80 398 81 227 82 273 83 319 84 177 85 272 86 204 87 139 88 187 89 263 90 90 91 134 92 67 93 115 94 45 95 65 96 40 97 108 98 60 99 102 total 100 blocks 29608 cartons I want to add loadgroup column to df2, try to keep those proportions as close as possible. How to do it please? Thank you very much for the help.
I don't know how to find loadgroup column based on both cartons percent and blocks percent. But generate random loadgroup based on either cartons percent or blocks percent is easy. Here is what I did. I generate 100,000 seeds first, then for each seed, I add column loadgroup1 based on cartons percent, loadgroup2 based on blocks percent, then calculate both percentages, then compare with df1 percentages, get absolute difference, record it. For these 100,000 seeds, I take the minimum difference one as my solution, which is sufficient for my job. But this is not the optimal solution, and I am looking for quick and easy way to do this. Hope somebody can help. Here is my code. df = pd.DataFrame() np.random.seed(10000) seeds = np.random.randint(1, 1000000, size = 100000) for i in range(46530, 46537): print(seeds[i]) np.random.seed(seeds[i]) df2['loadGroup1'] = np.random.choice(df1.loadgroup, len(df2), p = df1.CartonsPercent) df2['loadGroup2'] = np.random.choice(df1.loadgroup, len(df2), p = df1.blocksPercent) df2.reset_index(inplace = True) three = pd.DataFrame(df2.groupby('loadGroup1').agg(Cartons = ('cartons', 'sum'), blocks = ('block', 'count'))) three['CartonsPercent'] = three.Cartons/three.Cartons.sum() three['blocksPercent'] = three.blocks/three.blocks.sum() four = df1[['CartonsPercent','blocksPercent']] - three[['CartonsPercent','blocksPercent']] four = four.abs() subdf = pd.DataFrame({'i':[i],'Seed':[seeds[i]], 'Percent':['CartonsPercent'], 'AbsDiff':[four.sum().sum()]}) df = pd.concat([df,subdf]) three = pd.DataFrame(df2.groupby('loadGroup2').agg(Cartons = ('cartons', 'sum'), blocks = ('block', 'count'))) three['CartonsPercent'] = three.Cartons/three.Cartons.sum() three['blocksPercent'] = three.blocks/three.blocks.sum() four = df1[['CartonsPercent','blocksPercent']] - three[['CartonsPercent','blocksPercent']] four = four.abs() subdf = pd.DataFrame({'i':[i],'Seed':[seeds[i]], 'Percent':['blocksPercent'], 'AbsDiff':[four.sum().sum()]}) df = pd.concat([df,subdf]) df.sort_values(by = 'AbsDiff', ascending = True, inplace = True) df = df.head(10) Actually the first row of df will tell me the seed I am looking for, I kept 10 rows just for curiosity. Here is my solution. block cartons loadgroup 0 533 4 1 257 1 2 96 4 3 104 4 4 130 4 5 71 2 6 68 1 7 87 4 8 99 4 9 51 4 10 291 4 11 119 2 12 274 2 13 316 4 14 87 4 15 149 5 16 120 3 17 222 2 18 100 2 19 148 2 20 192 3 21 188 4 22 293 1 23 120 2 24 224 4 25 449 1 26 385 5 27 395 3 28 418 1 29 423 4 30 244 5 31 327 1 32 337 5 33 249 4 34 528 1 35 528 1 36 494 5 37 540 3 38 368 2 39 533 4 40 614 5 41 462 4 42 350 5 43 618 4 44 463 2 45 552 1 46 397 3 47 401 3 48 397 1 49 365 1 50 475 4 51 379 1 52 541 1 53 488 2 54 383 2 55 354 1 56 760 5 57 327 4 58 211 2 59 356 5 60 552 4 61 401 1 62 320 1 63 368 3 64 311 3 65 421 2 66 458 5 67 278 4 68 504 5 69 385 4 70 242 4 71 413 1 72 246 2 73 465 5 74 386 4 75 231 1 76 154 4 77 294 4 78 275 1 79 169 4 80 398 4 81 227 4 82 273 1 83 319 3 84 177 4 85 272 5 86 204 3 87 139 1 88 187 4 89 263 4 90 90 4 91 134 4 92 67 3 93 115 3 94 45 2 95 65 2 96 40 4 97 108 2 98 60 2 99 102 1 Here are the summaries. loadgroup cartons blocks cartonsPercent blocksPercent 1 7610 22 26% 22% 2 3912 18 13% 18% 3 3429 12 12% 12% 4 9269 35 31% 35% 5 5388 13 18% 13% It's very close to my target though.
pandas how to filter and slice with multiple conditions
Using pandas, how do I return dataframe filtered by value of 2 in 'GEN' column, value 20 in 'AGE' column and exclude columns with name 'GEN' and 'BP'? Thanks in advance:) AGE GEN BMI BP S1 S2 S3 S4 S5 S6 Y 59 2 32.1 101 157 93.2 38 4 4.8598 87 151 48 1 21.6 87 183 103.2 70 3 3.8918 69 75 72 2 30.5 93 156 93.6 41 4 4.6728 85 141 24 1 25.3 84 198 131.4 40 5 4.8903 89 206 50 1 23 101 192 125.4 52 4 4.2905 80 135 23 1 22.6 89 139 64.8 61 2 4.1897 68 97 20 2 22 90 160 99.6 50 3 3.9512 82 138 66 2 26.2 114 255 185 56 4.5 4.2485 92 63 60 2 32.1 83 179 119.4 42 4 4.4773 94 110 20 1 30 85 180 93.4 43 4 5.3845 88 310
You can do this - cols = df.columns[~df.columns.isin(['GEN','BP'])] out=df.loc[(df['GEN'] == 2) & (df['AGE'] == 20),cols] OR out=df.query("'GEN'==2 and 'AGE'==20").loc[cols]
How to interpret the log output of docplex optimisation library
I am having a problem interpreting this log that I get after trying to maximise an objective function using docplex: Nodes Cuts/ Node Left Objective IInf Best Integer Best Bound ItCnt Gap 0 0 6.3105 0 10.2106 26 0 0 5.9960 8 Cone: 5 34 0 0 5.8464 5 Cone: 8 47 0 0 5.8030 11 Cone: 10 54 0 0 5.7670 12 Cone: 13 64 0 0 5.7441 13 Cone: 16 72 0 0 5.7044 9 Cone: 19 81 0 0 5.6844 14 5.6844 559 * 0+ 0 4.5362 5.6844 25.31% 0 0 5.5546 15 4.5362 Cuts: 322 1014 22.45% 0 0 5.4738 15 4.5362 Cuts: 38 1108 20.67% * 0+ 0 4.6021 5.4738 18.94% 0 0 5.4296 16 4.6021 Cuts: 100 1155 17.98% 0 0 5.3779 19 4.6021 Cuts: 34 1204 16.86% 0 0 5.3462 17 4.6021 Cuts: 80 1252 16.17% 0 0 5.3396 19 4.6021 Cuts: 42 1276 16.03% 0 0 5.3364 24 4.6021 Cuts: 57 1325 15.96% 0 0 5.3269 17 4.6021 Cuts: 66 1353 15.75% 0 0 5.3188 20 4.6021 Cuts: 42 1369 15.57% 0 0 5.2975 21 4.6021 Cuts: 62 1387 15.11% 0 0 5.2838 24 4.6021 Cuts: 72 1427 14.81% 0 0 5.2796 21 4.6021 Cuts: 70 1457 14.72% 0 0 5.2762 24 4.6021 Cuts: 73 1471 14.65% 0 0 5.2655 24 4.6021 Cuts: 18 1479 14.42% * 0+ 0 4.6061 5.2655 14.32% * 0+ 0 4.6613 5.2655 12.96% 0 0 5.2554 26 4.6613 Cuts: 40 1492 12.75% 0 0 5.2425 27 4.6613 Cuts: 11 1511 12.47% 0 0 5.2360 23 4.6613 Cuts: 3 1518 12.33% 0 0 5.2296 19 4.6613 Cuts: 7 1521 12.19% 0 0 5.2213 18 4.6613 Cuts: 8 1543 12.01% 0 0 5.2163 24 4.6613 Cuts: 15 1552 11.91% 0 0 5.2106 21 4.6613 Cuts: 4 1558 11.78% 0 0 5.2106 21 4.6613 Cuts: 3 1559 11.78% * 0+ 0 4.6706 5.2106 11.56% 0 2 5.2106 21 4.6706 5.2106 1559 11.56% Elapsed time = 9.12 sec. (7822.43 ticks, tree = 0.01 MB, solutions = 5) 51 29 4.9031 3 4.6706 5.1575 1828 10.42% 260 147 4.9207 1 4.6706 5.1575 2699 10.42% 498 242 infeasible 4.6706 5.0909 3364 9.00% 712 346 4.7470 6 4.6706 5.0591 4400 8.32% 991 497 4.7338 6 4.6706 5.0480 5704 8.08% 1358 566 4.8085 11 4.6706 5.0005 7569 7.06% 1708 708 4.7638 14 4.6706 4.9579 9781 6.15% 1985 817 cutoff 4.6706 4.9265 11661 5.48% 2399 843 infeasible 4.6706 4.9058 15567 5.04% 3619 887 4.7066 4 4.6706 4.7875 23685 2.50% Elapsed time = 17.75 sec. (10933.85 ticks, tree = 3.05 MB, solutions = 5) 4623 500 4.6863 13 4.6706 4.7274 35862 1.22% What I don't understand is the following: What is the difference between the third (Objective) and fifth column (Best integer ) How come that the third column (Objective) has higher values than the actual solution of the problem given by CPLEX which is (4.6706) Does the values in the third column take into consideration the constraints given to the optimization problem? This webpage didn't help me to understand neither, the explanation of Best Integer is really confusing. Thank you in advance for your feedback. Regards.
The user manual includes a detailed explanation of this log in section CPLEX->User's Manual for CPLEX->Discrete Optimization->Solving Mixed Integer Programming Problems (MIP)->Progress Reports: interpreting the node log (see https://www.ibm.com/support/knowledgecenter/SSSA5P_12.8.0/ilog.odms.cplex.help/CPLEX/UsrMan/topics/discr_optim/mip/para/52_node_log.html)
I suggest to have a look at in https://fr.slideshare.net/mobile/IBMOptimization/2013-11-informsminingthenodelog
For loop and Conditionals in pandas DataFrame
I have this DataFrame and I need to write a conditionals to extract only the starred Energy. Energy Time 0 80 0 82 0 84 0 86 287** 88 287 90 287 91 287 92 0 94 0 96 0 98 302** 100 302 102 302 104 302 106 0 108 0 110 The idea is to accepts Energy values when Energy(t)>0 & Energy(t-1)==0. I tried creating a for loop set_time=set(df.loc[:,'Time']) list_time=list(set_time) for t, time in enumerate(list_time): if df['Energy'][t] > 0 & df['Energy'][t-1]==0: print(df['Energy'],t) But I get errors. Thanks a lot in advance
There is one way v=df.eq(0).Energy.cumsum() df.groupby(df.eq(0).Energy.cumsum()).head(2).loc[df.groupby(v).Energy.filter(lambda x : x.ne(0).any()).index].dropna() Out[1538]: Energy Time 3 0.0 86.0 4 287.0 88.0 10 0.0 98.0 11 302.0 100.0
Group clause in SQL command
I have 3 tables: Deliveries, IssuedWarehouse, ReturnedStock. Deliveries: ID, OrderNumber, Material, Width, Gauge, DelKG IssuedWarehouse: OrderNumber, IssuedKG ReturnedStock: OrderNumber, IssuedKG What I'd like to do is group all the orders by Material, Width and Gauge and then sum the amount delivered, issued to the warehouse and issued back to stock. This is the SQL that is really quite close: SELECT DELIVERIES.Material, DELIVERIES.Width, DELIVERIES.Gauge, Count(DELIVERIES.OrderNo) AS [Orders Placed], Sum(DELIVERIES.DeldQtyKilos) AS [KG Delivered], Sum(IssuedWarehouse.[Qty Issued]) AS [Film Issued], Sum([Film Retns].[Qty Issued]) AS [Film Returned], [KG Delivered]-[Film Issued]+[Film Returned] AS [Qty Remaining] FROM (DELIVERIES INNER JOIN IssuedWarehouse ON DELIVERIES.OrderNo = IssuedWarehouse.[Order No From]) INNER JOIN [Film Retns] ON DELIVERIES.OrderNo = [Film Retns].[Order No From] GROUP BY Material, Width, Gauge, ActDelDate HAVING ActDelDate Between [start date] And [end date] ORDER BY DELIVERIES.Material; This groups the products almost perfectly. However if you take a look at the results: Material Width Gauge Orders Placed Delivered Qnty Kilos Film Issued Film Returned Qty Remaining COEX-GLOSS 590 75 1 534 500 124 158 COEX-MATT 1080 80 1 4226 4226 52 52 CPP 660 38 8 6720 2768 1384 5336 CPP 666 47 1 5677 5716 536 497 CPP 690 65 2 1232 717 202 717 CPP 760 38 3 3444 1318 510 2636 CPP 770 38 4 4316 3318 2592 3590 CPP 786 38 2 672 442 212 442 CPP 800 47 1 1122 1122 116 116 CPP 810 47 1 1127 1134 69 62 CPP 810 47 2 2250 1285 320 1285 CPP 1460 38 12 6540 4704 2442 4278 LD 975 75 1 502 502 182 182 LDPE 450 50 1 252 252 50 50 LDPE 520 70 1 250 250 95 95 LDPE 570 65 2 504 295 86 295 LDPE 570 65 2 508 278 48 278 LDPE 620 50 1 252 252 67 67 LDPE 660 50 1 256 256 62 62 LDPE 670 75 1 248 248 80 80 LDPE 690 47 1 476 476 390 390 LDPE 790 38 2 2104 1122 140 1122 LDPE 790 50 1 286 286 134 134 LDPE 790 50 1 250 250 125 125 LDPE 810 30 1 4062 4062 100 100 LDPE 843 33 1 408 408 835 835 LDPE 850 80 1 412 412 34 34 LDPE 855 30 1 740 740 83 83 LDPE 880 60 1 304 304 130 130 LDPE 900 70 2 1000 650 500 850 LDPE 1017 60 1 1056 1056 174 174 OPP 25 1100 1 381 381 95 95 OPP 1000 30 2 1358 1112 300 546 OPP 1000 30 1 1492 1491 100 101 OPP 1200 20 1 418 417 461 462 PET 760 12 3 1227 1876 132 -517 You'll see that there are some materials that have the same width and gauge yet they are not grouped. I think this is because the delivered qty is different on the orders. For example: Material Width Gauge Orders Placed Delivered Qnty Kilos Film Issued Film Returned Qty Remaining LDPE 620 50 1 252 252 67 67 LDPE 660 50 1 256 256 62 62 I would like these two rows to be grouped. They have the same material, width and gauge but the delivered qty is different therefore it hasn't grouped it. Can anyone help me group these strange rows?
Your "problem" is that the deliveries occurred on different dates, and you're grouping by ActDelDate so the data splits, but because you haven't selected the ActDelDate column, this isn't obvious. The fix is: Remove ActDelDate from the group by list You should also remove the unnecessary brackets around the first join, and change HAVING ActDelDate Between [start date] And [end date] to WHERE ActDelDate Between [start date] And [end date] and have it before the GROUP BY
You are grouping by the delivery date, which is causing the rows to be split. Either omit the delivery date from the results and group by, or take the min/max of the delivery date.