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
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.