Предполагая, что в вашем реальном примере вы не можете просто ORDER BY i_item
или ORDER BY i_count
получить желаемый заказ, тогда:
SQL Fiddle
Настройка схемы Oracle 11g R2 :
CREATE TABLE abc ( i_id, i_item, i_count ) AS
SELECT 123, 'apple', 100 FROM DUAL UNION ALL
SELECT 123, 'banana', 200 FROM DUAL UNION ALL
SELECT 123, 'candy', 300 FROM DUAL;
CREATE TABLE def ( j_id, j_item, j_count ) AS
SELECT 4567, 'apple', 100 FROM DUAL UNION ALL
SELECT 4567, 'banana', 200 FROM DUAL UNION ALL
SELECT 4567, 'candy', 300 FROM DUAL;
Запрос 1 :
SELECT i_id,
i_item,
i_count
FROM (
SELECT i_id,
i_item,
i_count,
ROWNUM,
1
FROM abc
WHERE i_id = 123
UNION ALL
SELECT j_id,
j_item,
j_count,
ROWNUM,
2
FROM def
WHERE j_id = 4567
ORDER BY 4, 5
)
Результаты :
| I_ID | I_ITEM | I_COUNT |
|------|--------|---------|
| 123 | apple | 100 |
| 4567 | apple | 100 |
| 123 | banana | 200 |
| 4567 | banana | 200 |
| 123 | candy | 300 |
| 4567 | candy | 300 |