I would like to preserve the column order from a subquery that uses UNION to gather column values (refining the question I posed here [SQLite alternative to transposition / sub-select returns N columns - expected 1](https://stackoverflow.com/questions/70575473/sqlite-alternative-to-transposition-sub-select-returns-n-columns-expected-1)). The query below sorts numerically in ascending order of the keys, and I can't figure out a way to preserve their column order from the `Keys` table without introducing another attribute.

A minimal example:

```sql
CREATE TABLE Keys(primary_id, id1, id2, id3);
CREATE TABLE Attr(id, attr1, attr2);

INSERT INTO Keys VALUES(1, 7, 2, 5);
INSERT INTO Keys VALUES(2, 6, 1, 3);
INSERT INTO Keys VALUES(3, 4, 2, 7);
INSERT INTO Attr VALUES(1, "a", "b");
INSERT INTO Attr VALUES(2, "c", "d");
INSERT INTO Attr VALUES(3, "e", "f");
INSERT INTO Attr VALUES(4, "g", "h");
INSERT INTO Attr VALUES(5, "i", "j");
INSERT INTO Attr VALUES(6, "k", "l");
INSERT INTO Attr VALUES(7, "m", "n");

SELECT *
FROM Attr
WHERE Attr.id IN (SELECT primary_id FROM Keys WHERE Keys.primary_id=2
                  UNION
                  SELECT id1 FROM Keys WHERE Keys.primary_id=2
                  UNION
                  SELECT id2 FROM Keys WHERE Keys.primary_id=2
                  UNION
                  SELECT id3 FROM Keys WHERE Keys.primary_id=2);

1|a|b
2|c|d
3|e|f
6|k|l

```

Desired output preserving the ordering of `primary_id, id1, id2, id3`:

```sql
2|c|d
6|k|l
1|a|b
3|e|f

```

The subquery by itself I could sort by creating another attribute:

```sql
SELECT primary_id, 1 as sort FROM Keys WHERE Keys.primary_id = 2
UNION
SELECT id1, 2 as sort FROM Keys WHERE Keys.primary_id = 2
UNION
SELECT id2, 3 as sort FROM Keys WHERE Keys.primary_id = 2
UNION
SELECT id3, 4 as sort FROM Keys WHERE Keys.primary_id = 2
ORDER BY sort

```

But the outer query expects only 1 column, so this can't be a solution. Thank you for your ideas!