Suppose I have a super-simple table (called useful_table
) with one column, like the below:
+----------+
| a_number |
+----------+
| 2 |
| 4 |
| 8 |
+----------+
And suppose I have a query with a UNION
:
SELECT
a_number
FROM
useful_table
WHERE
a_number = 2
UNION
SELECT
a_number + 1
FROM
useful_table
WHERE
a_number = 2
;
I expect the result to be:
+----------+
| a_number |
+----------+
| 2 |
| 3 |
+----------+
Example query runs in Oracle, but should be applicable to other dialects.
Setup code here:
create table useful_table(a_number NUMBER);
insert into useful_table values (2);
insert into useful_table values (4);
insert into useful_table values (8);
Is it possible to convert this query?
Is it always possible to convert UNION
s to an alternative?
Assumptions:
- I am using a subset of (Oracle) SQL, meaning:
- I cannot use temporary tables.
- I cannot use
UNION
orUNION ALL
.
- Both sides of the
UNION
access data from the same table, but may change the raw data in different ways.