Code :
CREATE TABLE inv ( idinv numeric(18,0) NOT NULL PRIMARY KEY, codpro char(4) NOT NULL, qteinv numeric(18,0) NOT NULL, datinv char(8) NOT NULL ) go INSERT INTO inv (idinv, codpro, qteinv, datinv) VALUES (1, 'cas1', -5, '20070523'); INSERT INTO inv (idinv, codpro, qteinv, datinv) VALUES (2, 'cas1', 4, '20070523'); INSERT INTO inv (idinv, codpro, qteinv, datinv) VALUES (4, 'cas2', -5, '20070523'); INSERT INTO inv (idinv, codpro, qteinv, datinv) VALUES (3, 'cas3', 5, '20070523'); go SELECT isnull(v1.datinv, v2.datinv) date, isnull(v1.codpro, v2.codpro) produit, isnull(v1.qteinv * -1, 0) avant, isnull(v2.qteinv, 0) après FROM inv v1 full OUTER JOIN inv v2 ON v2.idinv > v1.idinv AND v2.datinv = v1.datinv AND v2.codpro = v1.codpro WHERE -- Filtre sur la date (v1.datinv = '20070523' OR v1.datinv IS NULL) AND ( -- Filtre sur le cas 1 v1.qteinv < 0 AND v2.qteinv > 0 -- Filtre sur le cas 2 OR v1.qteinv < 0 AND v2.qteinv IS NULL -- Filtre sur le cas 3 OR v1.qteinv IS NULL AND v2.qteinv > 0 ) go
|