CREATE TABLE test(
col1 INT IDENTITY(1,1) PRIMARY KEY,
col2 VARCHAR(25) NOT NULL,
col3 VARCHAR(25)
)
INSERT INTO test (col2,col3) VALUES ('6a','2')
INSERT INTO test (col2,col3) VALUES ('6b',NULL)
INSERT INTO test (col2,col3) VALUES ('6b',NULL)
INSERT INTO test (col2,col3) VALUES ('6a','3')
INSERT INTO test (col2,col3) VALUES ('6b',NULL)
INSERT INTO test (col2,col3) VALUES ('6b',NULL)
INSERT INTO test (col2,col3) VALUES ('6b',NULL)
INSERT INTO test (col2,col3) VALUES ('6a','f')
INSERT INTO test (col2,col3) VALUES ('6b',NULL)
SELECT * FROM test
SELECT T1.col1,
T1.col2,
T1.col3,
(SELECT col3 FROM test WHERE col1 = (SELECT MAX(T2.col1) FROM test T2 WHERE T2.col2 = '6a' AND T2.col1 < T1.col1)) NewCol3
FROM test T1
WHERE T1.col2 = '6b'
Si
SELECT *
INTO test_bkp
FROM test
UPDATE test
SET col3 = (SELECT col3 FROM test WHERE col1 = (SELECT MAX(T2.col1) FROM test T2 WHERE T2.col2 = '6a' AND T2.col1 < T1.col1))
FROM test T1
WHERE T1.col2 = '6b'
Am dubii cu privire la 2 aspecte:
[1] ordinea inregistrarilor care sunt transferate / importate inregistrarile. Aici trebuie sa gasesti o metoda pentru ca inregistrarile sa fie importate SIGUR CORECT (pentru a nu amesteca articolele dintr-o comanda cu articolele altei comenzi)
[2] de ce nu apelezi la o cheie externa ? Cheia externa iti va permite sa creezi legaturile intre inregistrarile COMANDA ( 6a ) si inregistrarile ARTICOLE COMANDATE ( 6b )