Using this sample dataset:
CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT, CCC VARCHAR (12));
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A123', 45, 123, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A120', 52, 120, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A133', 63, 133, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D123', 34, 123, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D120' ,32, 120, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D140', 12, 140, '2011-04' );
Im look for table with 3 column.
Col A “Id” order as Desc from orginal.
Col B “Id2” as previous or next row Id in col A where CCC are the same.
Id, Id2 CCC
A120 A123 '2011-03'
A123 A133 '2011-03'
A133 '2011-03'
D120 D123 '2011-04'
D123 D140 '2011-04'
D140 '2011-04'
or
Id, Id2 CCC
A120 '2011-03'
A123 A120 '2011-03'
A133 A123 '2011-03'
D120 '2011-04'
D123 D120 '2011-04'
D140 D123 '2011-04'
OR
Id, Id2 CCC
A123 A120 '2011-03'
A133 A123 '2011-03'
D123 D120 '2011-04'
D140 D123 '2011-04'
Could I add an autocrement col to joined table, then use 1, 2, 3 rows up or down? The id2 would then be based on this autocrement row?
Operating on Marc’s basis:
This might be helpful with more complex queries:
and then use it: