insert into pyramiddata_2tiles ( pyrdata_id, img_id, level, tilex_a, tiley_a, x_a, y_a, x0_a, y0_a, w_a, h_a, tilex_b, tiley_b, x_b, y_b, x0_b, y0_b, w_b, h_b, hash, tile_data ) select a.pyrdata_id as pyrdata_id ,a.img_id as img_id ,a.level as level ,a.tilex as tilex_a ,a.tiley as tiley_a ,a.x as x_a ,a.y as y_a ,a.x0 as x0_a ,a.y0 as y0_a ,a.w as w_a ,a.h as h_a ,b.tilex as tilex_b ,b.tiley as tilex_b ,a.x as x_b ,a.y as y_b ,b.x0 as x0_b ,b.y0 as y0_b ,b.w as w_b ,b.h as h_b ,a.hash as hash ,a.tile_data||b.tile_data as tile_data from ( select * from pyramiddata where level >= 3 and level <= 6 --and img_id = 1 and tile_data is not null ) a JOIN ( select * from pyramiddata where level >= 4 and level <= 7 --and img_id = 1 and tile_data is not null ) b on a.img_id = b.img_id and a.level + 1 = b.level and a.tilex / 2 = b.tilex and a.tiley / 2 = b.tiley