Split into multi rows based on target table

114
July 04, 2018, at 10:10 PM

I have two table where i want to join and show quantity with details. table are join with ITM,DIA , and total Qty is equal in both table on ITM/DIA combination

I want to split table2 quantity on table1 and populate table2 data along with table1 data.

I have below data for your reference, "table1" and "table2". and you can see my expected result in table "tableResult"

CREATE TABLE table1
    (`ITM` varchar(5), `DIA` varchar(4), `LOC` varchar(4), `ID` varchar(3), `QTY` int)
;
INSERT INTO table1
    (`ITM`, `DIA`, `LOC`, `ID`, `QTY`)
VALUES
    ('Item1', 'DIA1', 'LOC1', 'ID1', 3),
    ('Item1', 'DIA1', 'LOC2', 'ID2', 4),
    ('Item1', 'DIA1', 'LOC2', 'ID2', 6),
    ('Item1', 'DIA2', 'LOC2', 'ID2', 6),
    ('Item1', 'DIA2', 'LOC3', 'ID3', 18),
    ('Item1', 'DIA2', 'LOC4', 'ID4', 90),
    ('Item1', 'DIA2', 'LOC4', 'ID5', 23),
    ('Item1', 'DIA3', 'LOC5', 'ID6', 50),
    ('Item1', 'DIA3', 'LOC6', 'ID7', 20),
    ('Item2', 'DIA1', 'LOC4', 'ID8', 44),
    ('Item2', 'DIA2', 'LOC5', 'ID8', 21),
    ('Item2', 'DIA3', 'LOC6', 'ID9', 20)
;

CREATE TABLE table2
    (`ITM` varchar(5), `DIA` varchar(4), `NTA` varchar(5), `QTY` int)
;
INSERT INTO table2
    (`ITM`, `DIA`, `NTA`, `QTY`)
VALUES
    ('Item1', 'DIA1', 'NTA1', 10),
    ('Item1', 'DIA1', 'NTA2', 3),
    ('Item1', 'DIA2', 'NTA3', 30),
    ('Item1', 'DIA2', 'NTA4', 7),
    ('Item1', 'DIA2', 'NTA5', 100),
    ('Item1', 'DIA3', 'NTA6', 70),
    ('Item2', 'DIA1', 'NTA7', 22),
    ('Item2', 'DIA1', 'NTA8', 20),
    ('Item2', 'DIA2', 'NTA9', 6),
    ('Item2', 'DIA2', 'NTA10', 15),
    ('Item2', 'DIA3', 'NTA11', 8),
    ('Item2', 'DIA3', 'NTA11', 12)
;

CREATE TABLE tableResult
    (`ITM` varchar(5), `DIA` varchar(4), `LOC` varchar(4), `ID` varchar(3), `QTY` int, `NTA` varchar(5), `NewQTY` int)
;
INSERT INTO tableResult
    (`ITM`, `DIA`, `LOC`, `ID`, `QTY`, `NTA`, `NewQTY`)
VALUES
    ('Item1', 'DIA1', 'LOC1', 'ID1', 3, 'NTA1', 3),
    ('Item1', 'DIA1', 'LOC2', 'ID2', 4, 'NTA1', 4),
    ('Item1', 'DIA1', 'LOC2', 'ID2', 6, 'NTA1', 3),
    ('Item1', 'DIA1', 'LOC2', 'ID2', 6, 'NTA2', 3),
    ('Item1', 'DIA2', 'LOC2', 'ID2', 6, 'NTA3', 6),
    ('Item1', 'DIA2', 'LOC3', 'ID3', 18, 'NTA3', 18),
    ('Item1', 'DIA2', 'LOC4', 'ID4', 90, 'NTA3', 6),
    ('Item1', 'DIA2', 'LOC4', 'ID4', 90, 'NTA4', 7),
    ('Item1', 'DIA2', 'LOC4', 'ID4', 90, 'NTA5', 77),
    ('Item1', 'DIA2', 'LOC4', 'ID5', 23, 'NTA5', 23),
    ('Item1', 'DIA3', 'LOC5', 'ID6', 50, 'NTA6', 50),
    ('Item1', 'DIA3', 'LOC6', 'ID7', 20, 'NTA6', 20),
    ('Item2', 'DIA1', 'LOC4', 'ID8', 44, 'NTA7', 22),
    ('Item2', 'DIA1', 'LOC4', 'ID8', 44, 'NTA8', 20),
    ('Item2', 'DIA2', 'LOC5', 'ID8', 21, 'NTA9', 6),
    ('Item2', 'DIA2', 'LOC5', 'ID8', 21, 'NTA10', 15),
    ('Item2', 'DIA3', 'LOC6', 'ID9', 20, 'NTA11', 8),
    ('Item2', 'DIA3', 'LOC6', 'ID9', 20, 'NTA11', 12)
;

Could you please share your solution on this? appreciate lot on your valuable ideas..

READ ALSO
video does not plays after compression [on hold]

video does not plays after compression [on hold]

I was going to make video sharing app

127
Spinner not showing on device but is on emulators

Spinner not showing on device but is on emulators

My initial 'home' activity in my android App has a spinner populated from a databaseOn the emulator this works absolutely fine

133
how to create Excel sheets automatically every day?

how to create Excel sheets automatically every day?

I want to create Excel sheets with apache poi every day, while saving the other sheets if there are already some

146