How will i join/combine these two table to get one result - sql

Table 1
Code Name1 type BalanceDue id1 id2 id3 id4 id5 emp
2600 intl-Airfare 1 2.38 120 410 510 603 7060513 null
1100 intl-travel 1 2.66 120 420 540 602 7060513 null
2400 intl-Meals 1 1.50 120 420 520 602 7060513 null
4100 Transpo 2 19.70 110 210 510 601 null
4100 Transpo 2 13.25 110 210 500 601 null
4100 Transpo 2 17.38 110 210 500 600 null
3600 Dom travel 3 25.11 110 210 500 600 55713 null
Table 2
Code Details Total type code1 code2 code3 code4 code5 emp
4100 no#233 Emp1-Parking 11.39 2 110 210 510 601 null null
4100 no#231 Jes-Parking 6.83 2 110 210 510 601 null null
4100 no#232 Jes-TransExp 1.48 2 110 210 510 601 null null
4100 no#234 Emp2-TollFee 0.23 2 110 210 500 601 null null
4100 no#239 Emp2-Parking 1.82 2 110 210 500 601 null null
4100 no#240 Emp3-Parking 2.96 2 110 210 500 601 null null
4100 no#252 Emp5-TollFee 8.24 2 110 210 500 601 null null
4100 no#235 Jay-TollFee 4.90 2 110 210 500 600 null null
4100 no#243 Jay-TransExp 12.48 2 110 210 500 600 null null
I want this as a result:
if type is 1 then display all values in table 1 which has type 1
same as if type is 3
if type is 2 then display all values in table 2 considering code1 to code5
Result
Code Details type Total code1 code2 code3 code4 code5 emp
2600 intl-Airfare 1 2.38 120 410 510 603 7060513 null
1100 intl-travel 1 2.66 120 420 540 602 7060513 null
2400 intl-Meals 1 1.50 120 420 520 602 7060513 null
4100 no#233 Emp1-Parking 2 11.39 110 210 510 601 null null
4100 no#231 Jes-Parking 2 6.83 110 210 510 601 null null
4100 no#232 Jes-TransExp 2 1.48 110 210 510 601 null null
4100 no#234 Emp2-TollFee 2 0.23 110 210 500 601 null null
4100 no#239 Emp2-Parking 2 1.82 110 210 500 601 null null
4100 no#240 Emp3-Parking 2 2.96 110 210 500 601 null null
4100 no#252 Emp5-TollFee 2 8.24 110 210 500 601 null null
4100 no#235 Jay-TollFee 2 4.90 110 210 500 600 null null
4100 no#243 Jay-TransExp 2 12.48 110 210 500 600 null null
3600 Dom travel 3 25.11 110 210 500 600 55713 null
Sometimes, id5 has a value and sometimes other columns were null.
sorry, i hope i explained it clearly :(
Thanks everyone!

Your desired results look like a UNION (The UNION clause lets your "merge" together more queries in a single, final result set) similar to this:
SELECT Code, Name1 as Details, type, BalanceDue as Total,id1 as code1, id2 as code2,
id3 as code3, id4 as code4, id5 as code5, emp
FROM Table1 WHERE type IN (1, 3) -- could also be WHERE type = 1 OR type = 3
UNION
SELECT Code, Details, type, Total, code1, code2, code3, code4, code5, emp
FROM Table2 WHERE type = 2

Related

SQL to find related rows in Loop in ANSI SQL or Snowflake SQL

I have a requirement where I need to link all related CUSTOMER ID and assign a Unified Cust ID to all the related Cust_id.
Ex: for below data,
INPUT DATA
PK_ID CUST_ID_1 CUST_ID_2 CUST_ID_3
1 123 456 567
2 898 567 780
3 999 780 111
4 111 222 333
Based on CUST_ID_1/CUST_ID_2/CUST_ID_3 need to link all the and assign a Unified ID to all the rows.
OUTPUT DATA
Unified ID CUST_ID_1 CUST_ID_2 CUST_ID_3
1000 123 456 567
1000 898 567 780
1000 999 780 111
1000 111 222 333
Trying to perform Self Join but it cannot be definite. Is there a function or ANSI SQL feature which can help in this?
What i have tried,
CREATE TEMP TBL_TEMP AS(
SELECT A.PK_ID
FROM TBL A
LEFT JOIN TBL B
ON A.CUST_ID_1=B.CUST_ID_1
AND A.PK_ID<>B.PK_ID)
UPDATE TBL
FROM TBL_TEMP
SET UNIFIED_ID=SEQ_UNIF_ID.nextval
WHERE TBL.PK_ID=TBL_TEMP.PK_ID
This update i have to write for each column and multiple times.
If you are ok with gap in sequences then following is what I can come up with as of now.
update cust_temp a
set unified_id = t.unified_id
from
(
select
case
when (select count(*) from cust_temp b
where arrays_overlap(array_construct(a.cust_id_1,a.cust_id_2,a.cust_id_3),
array_construct(b.cust_id_1,b.cust_id_2,b.cust_id_3)))>1 -- match across data-set
then 1000 -- same value for common rows
else
ts.nextval --- using sequence for non-common rows
end unified_id,
a.cust_id_1,a.cust_id_2,a.cust_id_3
from cust_temp a, table(getnextval(SEQ_UNIF_ID)) ts) t
where t.cust_id_1 = a.cust_id_1
and t.cust_id_2 = a.cust_id_2
and t.cust_id_3 = a.cust_id_3;
Updated data-set
select * from cust_temp;
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
1000
123
456
567
1000
898
567
780
1000
111
222
333
20000
100
200
300
1000
999
780
111
1000
234
123
901
23000
260
360
460
24000
160
560
760
Original data set -
select * from cust_temp;
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
NULL
123
456
567
NULL
898
567
780
NULL
111
222
333
NULL
100
200
300
NULL
999
780
111
NULL
234
123
901
NULL
260
360
460
NULL
160
560
760
Arrays_overlap logic is thanks to #Simeon.
Following procedure can be used -
EXECUTE IMMEDIATE $$
DECLARE
duplicate number;
x number;
BEGIN
duplicate := (select count(cnt) from (select a.unified_id,count(*) cnt from cust_temp a,
cust_temp b
where
arrays_overlap(array_construct(a.cust_id_1,a.cust_id_2,a.cust_id_3),
array_construct(b.cust_id_1,b.cust_id_2,b.cust_id_3))
AND a.cust_id_1 != b.cust_id_1
AND a.cust_id_2 != b.cust_id_2
AND a.cust_id_3 != b.cust_id_3
group by a.unified_id) where cnt>1
);
for x in 1 to duplicate do
update cust_temp a
set a.unified_id = (select min(b.unified_id) uid from cust_temp b
where arrays_overlap(array_construct(a.cust_id_1,a.cust_id_2,a.cust_id_3),
array_construct(b.cust_id_1,b.cust_id_2,b.cust_id_3)));
end for;
END;
$$
;
Which will produce following output dataset -
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
1000
100
200
300
2000
123
456
567
2000
898
567
780
2000
111
222
333
2000
999
780
111
2000
234
123
901
7000
260
360
460
8000
160
560
760
8000
186
160
766
For an input data-set as -
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
1000
100
200
300
2000
123
456
567
3000
898
567
780
4000
111
222
333
5000
999
780
111
6000
234
123
901
7000
260
360
460
8000
160
560
760
9000
186
160
766

How to find the row associated with the min/max of a column?

So basically I have some simple SQL code that looks like the following;
SELECT
[Column1]
,[Column2]
,[Column3]
,[Column4]
,MIN([Column5]) AS maxColumn5
,MAX([Column6]) AS minColumn6
,SUM([Column7]) AS sumColumn7
,SUM([Column8]) AS sumColumn8
,SUM([Column9]) AS sumColumn9
FROM
[tableName]
GROUP BY
[Column1]
,[Column2]
,[Column3]
,[Column4]
What I am trying to do is also find the column either 'Column1', 'Column2', or 'Column3' that corresponds to the MIN([Column6]) and then the column that corresponds to MAX([Column8]).
The output should be exactly the same except there will be an extra 2 column at the end specifying which one the min and max are associated with.
I think there is a simple problem in your question, as Col1,Col2,Col3 that correspond to the max or min, are displayed directly, in other words you have them as you are grouping by Col1,Col2,Col3 & Col4.
As you did not provide some data, I will set some random data to prove my point.
Lets create a memory table similar to yours with 9 columns and fill it with random data for col6-8 with 10 rows for example, you can use the below:-
Declare #data Table(
Column1 int,Column2 int,Column3 int,Column4 int,Column5 int,Column6 int,Column7 int,Column8 int,Column9 int
)
declare #index int=5
while(#index>0)
begin
insert into #data values(1,2,3,4,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000)
insert into #data values(5,6,7,8,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000)
set #index=#index-1
end
we can see the data with the below
select * from #data order BY [Column1],[Column2],[Column3],[Column4]
Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9
1 2 3 4 669 203 278 364 577
1 2 3 4 389 316 290 548 661
1 2 3 4 835 555 942 985 604
1 2 3 4 477 743 580 305 414
1 2 3 4 431 296 471 150 352
1 2 3 4 346 220 573 941 633
1 2 3 4 392 450 652 978 883
1 2 3 4 235 479 751 136 978
1 2 3 4 906 183 141 915 783
1 2 3 4 329 342 682 977 870
5 6 7 8 218 740 41 299 816
5 6 7 8 800 630 674 888 799
5 6 7 8 27 307 446 743 345
5 6 7 8 501 928 824 592 691
5 6 7 8 439 624 260 757 547
5 6 7 8 287 610 287 708 652
5 6 7 8 441 711 433 642 343
5 6 7 8 751 928 237 53 535
5 6 7 8 594 768 708 173 33
5 6 7 8 352 703 943 867 661
now lets see the result of your grouping that you provided without any change
Col1 Col2 Col3 Col4 minCol5 maxCol6 maxCol8 sumCol7 sumCol8 sumCol9
1 2 3 4 235 743 985 5360 6299 6755
5 6 7 8 27 928 888 4853 5722 5422
so if we go back to your question, what is the value of Col1,Col2,Col3 for the maxCol6, well for each maxCol6 you have the values of Col1,Col2,Col3 & even Col4.
so what are the values for Col1,Col2,Col3 for maxCol16 that is 928, well they are 5,6 & 7.
ok, now lets say you want the record key that have that maxCol6, that is easy too, we would add an identity col as ID as below:-
Declare #data Table(
ID int identity(1,1), Column1 int,Column2 int,Column3 int,Column4 int,Column5 int,Column6 int,Column7 int,Column8 int,Column9 int
)
declare #index int=10
while(#index>0)
begin
insert into #data values(1,2,3,4,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000)
insert into #data values(5,6,7,8,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000,RAND()*1000)
set #index=#index-1
end
select * from #data order BY [Column1],[Column2],[Column3],[Column4]
;with agg as (
SELECT
[Column1]
,[Column2]
,[Column3]
,[Column4]
,MIN([Column5]) AS minColumn5
,MAX([Column6]) AS maxColumn6
,MAX([Column8]) AS maxColumn8
,SUM([Column7]) AS sumColumn7
,SUM([Column8]) AS sumColumn8
,SUM([Column9]) AS sumColumn9
FROM
#data [tableName]
GROUP BY
[Column1]
,[Column2]
,[Column3]
,[Column4]
)
--select * from agg order BY [Column1],[Column2],[Column3],[Column4]
select agg.*,maxCol6.ID [MaxCol6Seq],maxCol8.ID [MaxCol8Seq] from agg
inner join #data maxCol6
on agg.Column1=maxCol6.Column1
and agg.Column2=maxCol6.Column2
and agg.Column3=maxCol6.Column3
and agg.Column4=maxCol6.Column4
and agg.maxColumn6=maxCol6.Column6
inner join #data maxCol8
on agg.Column1=maxCol8.Column1
and agg.Column2=maxCol8.Column2
and agg.Column3=maxCol8.Column3
and agg.Column4=maxCol8.Column4
and agg.maxColumn8=maxCol8.Column8
As this is a new run for this set of data , below:-
ID Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9
1 1 2 3 4 201 848 993 50 304
3 1 2 3 4 497 207 644 399 104
5 1 2 3 4 445 321 822 151 185
7 1 2 3 4 611 402 620 61 543
9 1 2 3 4 460 409 182 915 211
11 1 2 3 4 886 804 180 213 282
13 1 2 3 4 614 709 932 806 162
15 1 2 3 4 795 752 110 474 463
17 1 2 3 4 737 545 77 648 727
19 1 2 3 4 788 862 266 464 851
20 5 6 7 8 218 561 943 572 54
18 5 6 7 8 741 621 610 214 536
16 5 6 7 8 579 248 374 693 761
14 5 6 7 8 866 415 198 528 657
12 5 6 7 8 905 947 500 50 387
10 5 6 7 8 492 860 948 299 220
8 5 6 7 8 861 328 727 40 327
6 5 6 7 8 435 534 707 769 777
4 5 6 7 8 587 68 45 184 614
2 5 6 7 8 189 24 289 121 772
The result is as below:-
C1 C2 C3 C4 minC5 maxC6 maxC8 sumC7 sumC8 sumC9 MaxCol6Seq MaxCol8Seq
1 2 3 4 201 862 915 4826 4181 3832 19 9
5 6 7 8 189 947 769 5341 3470 5105 12 6
Hope this helps.
If you just want a flag on each row specifying whether the value is the overall maximum or minimum, you can use window functions and CASE:
SELECT [Column1], [Column2], [Column3], [Column4],
MAX([Column5]) AS maxColumn5,
MIN([Column6]) AS minColumn6,
SUM([Column7]) AS sumColumn7,
SUM([Column8]) AS sumColumn8,
SUM([Column9]) AS sumColumn9,
(CASE WHEN MIN([Column6]) = MIN(MIN([Column6])) OVER () THEN 1 ELSE 0 END) as is_min_column6,
(CASE WHEN MAX([Column7]) = MAX(MAX([Column7])) OVER () THEN 1 ELSE 0 END) as is_max_column7
FROM [tableName]
GROUP BY [Column1], [Column2], [Column3], [Column4]

SQL Join by comparing measures or loop with cursors?

In order to verify if Deliveries are done on time, I need to match delivery Documents to PO schedule lines (SchLin) based on the comparison between Required Quantity (ReqQty) and Delivered Quantity (DlvQty).
The Delivery Docs have a reference to the PO and POItm but not to the SchLin.
Once a Delivery Doc is assigned to a Schedule Line I can calculate the Delivery Delta (DlvDelta) as the number of days it was delivered early or late compared to the requirement (ReqDate).
Examples of the two base tables are as follows:
Schedule lines
PO POItm SchLin ReqDate ReqQty
123 1 1 10/11 20
123 1 2 30/11 30
124 2 1 15/12 10
124 2 2 24/12 15
Delivery Docs
Doc Item PO POItm DlvDate DlvQty
810 1 123 1 29/10 12
816 1 123 1 02/11 07
823 1 123 1 04/11 13
828 1 123 1 06/11 08
856 1 123 1 10/11 05
873 1 123 1 14/11 09
902 1 124 2 27/11 05
908 1 124 2 30/11 07
911 1 124 2 08/12 08
923 1 124 2 27/12 09
Important: Schedule Lines and Deliveries should have the same PO and POItm.
The other logic to link is to sum the DlvQty until we reach (or exceed) ReqQty.
Those deliveries are then linked to the schedule line. Subsequent deliveries are used for the following schedule line(s). A delivery schould be matched to only one schedule line.
After comparing the ReqQty and DlvQty the assignments should result in following:
Result
Doc Item PO POItm Schlin ReqDate DlvDate DlvDelta
810 1 123 1 1 10/11 29/10 -11
816 1 123 1 1 10/11 02/11 -08
823 1 123 1 1 10/11 04/11 -06
828 1 123 1 2 30/11 06/11 -24
856 1 123 1 2 30/11 10/11 -20
873 1 123 1 2 30/11 14/11 -16
902 1 124 2 1 15/12 27/11 -18
908 1 124 2 1 15/12 30/11 -15
911 1 124 2 2 24/12 08/12 -16
923 1 124 2 2 24/12 27/12 +03
Up till now, I have done this with loops using cursors but performance is rather sluggish.
Is there another way in SQL (script) using e.g. joins by comparing measures to achieve the same result?
Regards,
Eric
If you can express the rule for matching a delivery with a schedule line, you can produce the results you want in a single query. And, yes, I promise it will be faster (and simpler) than executing the same logic in loops on cursors.
I can't reproduce your exact results because I don't quite understand how the two tables relate. Hopefully from the code below you'll be able to figure it out by adjusting the join criteria.
I don't have your DBMS. My code uses SQLite, which has its own peculiar date functions. You'll have to substitute the ones your system provides. In any event, I can't recommend 5-character strings for dates. Use a datetime type if you have one, and include 4-digit years regardless. Else how many days are there between Christmas and New Years Day?
create table S (
PO int not NULL,
POItm int not NULL,
SchLin int not NULL,
ReqDate char not NULL,
ReqQty int not NULL,
primary key (PO, POItm, SchLin)
);
insert into S values
(123, 1, 1, '10/11', 20 ),
(123, 1, 2, '30/11', 30 ),
(124, 2, 1, '15/12', 10 ),
(124, 2, 2, '24/12', 15 );
create table D (
Doc int not NULL,
Item int not NULL,
PO int not NULL,
POItm int not NULL,
DlvDate char not NULL,
DlvQty int not NULL,
primary key (Doc, Item)
);
insert into D values
(810, 1, 123, 1, '29/10', 12 ),
(816, 1, 123, 1, '02/11', 07 ),
(823, 1, 123, 1, '04/11', 13 ),
(828, 1, 123, 1, '06/11', 08 ),
(856, 1, 123, 1, '10/11', 05 ),
(873, 1, 123, 1, '14/11', 09 ),
(902, 1, 124, 2, '27/11', 05 ),
(908, 1, 124, 2, '30/11', 07 ),
(911, 1, 124, 2, '08/12', 08 ),
(923, 1, 124, 2, '27/12', 09 );
select D.Doc, D.Item, D.PO, S.SchLin, S.ReqDate, D.DlvDate
, cast(
julianday('2018-' || substr(DlvDate, 4,2) || '-' || substr(DlvDate, 1,2))
- julianday('2018-' || substr(ReqDate, 4,2) || '-' || substr(ReqDate, 1,2))
as int) as DlvDelta
from S join D on S.PO = D.PO and S.POItm = D.POItm
;
Result:
Doc Item PO SchLin ReqDate DlvDate DlvDelta
---------- ---------- ---------- ---------- ---------- ---------- ----------
810 1 123 1 10/11 29/10 -12
810 1 123 2 30/11 29/10 -32
816 1 123 1 10/11 02/11 -8
816 1 123 2 30/11 02/11 -28
823 1 123 1 10/11 04/11 -6
823 1 123 2 30/11 04/11 -26
828 1 123 1 10/11 06/11 -4
828 1 123 2 30/11 06/11 -24
856 1 123 1 10/11 10/11 0
856 1 123 2 30/11 10/11 -20
873 1 123 1 10/11 14/11 4
873 1 123 2 30/11 14/11 -16
902 1 124 1 15/12 27/11 -18
902 1 124 2 24/12 27/11 -27
908 1 124 1 15/12 30/11 -15
908 1 124 2 24/12 30/11 -24
911 1 124 1 15/12 08/12 -7
911 1 124 2 24/12 08/12 -16
923 1 124 1 15/12 27/12 12
923 1 124 2 24/12 27/12 3

Sum or join rows by criteria

Suppose I have a table like this:
Table1
EmployeeID Month sym Quantity V_M BudjetCode
222222 1 40 133.35 1214 800000
222222 2 40 178.50 115 800000
222222 3 40 150.33 215 800000
222222 4 40 186.37 315 800000
222222 5 40 127.38 415 800000
222222 6 40 153.00 515 800000
222222 7 40 178.50 615 800000
222222 7 40 8.50 615 700052015
222222 8 40 187.00 715 800000
And I would like to change this table to:
Table 2
EmployeeID Month sym Quantity V_M BudjetCode
222222 1 40 133.35 1214 800000
222222 2 40 178.50 115 800000
222222 3 40 150.33 215 800000
222222 4 40 186.37 315 800000
222222 5 40 127.38 415 800000
222222 6 40 161.5 515 800000
222222 7 40 178.50 615 800000
222222 8 40 187.00 715 800000
How?
See the row in table 1 where the BudjetCode is unusual?
well, for this row I would like to add the Quantity value (8.5) to the row there V_M is one less than the V_M is the original row (where the budjetCode is 700052015).
In the example, in the original it was 615 so one less is 515 (615 means date 6.15 and 515 means 5.15) and to that I need to add the quantity (8.5 to 153 = 161.5)
I was thinking of "over partition":
select [EmployeeID],[Month],[Sym],
sum([Quantity])
over (partition by [EmployeeID], [V_M]-1 where???) as b
from table1
where [Sym] = '40' and [EmployeeID] = 222222
order by [Month]
But I don't know how to sum this up usiing the criteria of the budjetCode starts with "700".
comment : Don't to this for the first row.
Update:
EmployeeID month Quantity V_M MS_BudjetCode
22222 1 40 133.35 1214 88888888
22222 2 40 178.50 115 88888888
22222 3 40 150.33 215 88888888
22222 4 40 186.37 315 88888888
22222 5 40 127.38 415 88888888
22222 6 40 153.00 515 88888888
22222 7 40 8.50 615 700000000
22222 7 40 178.50 615 88888888
22222 8 40 187.00 715 88888888
Output:
22222 2 40 178.50 115 88888888
22222 4 40 186.37 315 88888888
22222 8 40 187.00 715 88888888
22222 3 40 151.33 215 88888888
22222 3 40 151.33 215 88888888
22222 3 40 151.33 215 88888888
22222 3 40 149.33 215 88888888
22222 3 40 149.33 215 88888888
22222 5 40 127.38 415 88888888
22222 6 40 152.00 515 88888888
22222 6 40 154.00 515 88888888
22222 6 40 152.00 515 88888888
22222 6 40 154.00 515 88888888
22222 6 40 154.00 515 88888888
22222 6 40 152.00 515 88888888
22222 6 40 161.50 515 88888888
22222 7 40 178.50 615 88888888
22222 1 40 133.35 1214 88888888
Query:
SELECT t1.EmployeeID, t1.Month, t1.sym,
t1.Quantity + COALESCE(t2.Quantity, 0),
t1.V_M, t1.BudjetCode
FROM Table1 AS t1
LEFT JOIN Table1 AS t2
ON t1.EmployeeID = t2.EmployeeID AND t1.V_M = t2.V_M - 100 AND
SUBSTRING(t2.BudjetCode,1,3) = '700'
WHERE SUBSTRING(t1.BudjetCode,1,3) <> '700' and sym='40' and EmployeeID = 22222
This may be a solution, i think we cant simply subtract 1 with month, need to consider january december cases also. i had included that checking also. here budgetcode is distinguished with length, you can change it if needed.
;WITH CTETable1 AS
(
SELECT
EmployeeID,[Month],sym,
CASE WHEN LEN(BudjetCode)>6 THEN Quantity ELSE 0 END Quantity,
CASE WHEN LEN(BudjetCode)>6 THEN
(CASE WHEN LEFT(V_M,LEN(V_M)-2)=1 THEN 12 ELSE LEFT(V_M,LEN(V_M)-2)-1 END)*100+
(CASE WHEN LEFT(V_M,LEN(V_M)-2)=1 THEN RIGHT(V_M,2)-1 ELSE RIGHT(V_M,2) END) ELSE 0 END V_M,
BudjetCode
FROM Table1
)
SELECT
T1.EmployeeID,T1.[Month],T1.sym,T1.Quantity+ISNULL(T2.Quantity,0) Quantity,
T1.V_M,T1.BudjetCode
FROM Table1 T1
LEFT JOIN CTETable1 T2 ON T1.EmployeeID=T2.EmployeeID AND T1.V_M=T2.V_M
WHERE LEN(T1.BudjetCode)=6
Will this solve your problem?
DELETE
FROM
table
WHERE BudjetCode='700052015'
AND
UPDATE table
SET Quantity = (Quantity+8.5)
WHERE Month = 6;
I think you can do what you want using a simple LEFT JOIN operation:
SELECT t1.EmployeeID, t1.Month, t1.sym,
t1.Quantity + COALESCE(t2.Quantity, 0),
t1.V_M, t1.BudjetCode
FROM Table1 AS t1
LEFT JOIN Table1 AS t2
ON t1.EmployeeID = t2.EmployeeID AND t1.V_M = t2.V_M - 100 AND
SUBSTRING(t2.BudjetCode,1,3) = '700'
WHERE SUBSTRING(t1.BudjetCode,1,3) <> '700'
The query select all table rows but the 'unusual' ones and joins them with 'unusual' rows one day (or is it maybe hour?) ahead. Quantity returned is the sum of the value of 'normal' row plus the value of the related 'unusual' row (if such one really exists).
Expression:
t1.V_M = t2.V_M - 100
implements one less relation between V_Mof t1 and t2. You may change it to suit your actual needs.
Demo here

Retrieving data from unnormalized vertically ordered table

I have the following relation:
employeevalue(id, name, value, code)
id name value code
101 bobby 150 100
101 bobby 12 150
101 bobby 14.6 200
102 mary 189 100
102 mary 128 150
102 mary 112 200
103 john 112 100
103 john 13 150
103 john 76 200
Where code 100 is value1, 150 is value2 and 200 is value3. How could I write an SQL statement to retrieve the following from this table?
id name value1 value2 value3
101 bobby 150 12 14.6
102 mary 189 128 112
103 john 112 13 76
You can do this with conditional aggregation:
select id,
max(case when code = 100 then value end) as value1,
max(case when code = 150 then value end) as value2,
max(case when code = 200 then value end) as value3
from table t
group by id;