mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-10-17 22:10:54 +00:00
aa17da6b68
* [docs] adjust SELECT ToC meta * Update array-join.md * Update distinct.md * Update format.md * Update from.md * Update group-by.md * Update having.md * Update into-outfile.md * Update join.md * Update limit-by.md * Update limit.md * Update order-by.md * Update prewhere.md * Update sample.md * Update union-all.md * Update where.md * Update with.md
35 lines
1.4 KiB
Markdown
35 lines
1.4 KiB
Markdown
---
|
||
toc_title: UNION ALL
|
||
---
|
||
|
||
# UNION ALL Clause {#union-all-clause}
|
||
|
||
You can use `UNION ALL` to combine any number of `SELECT` queries by extending their results. Example:
|
||
|
||
``` sql
|
||
SELECT CounterID, 1 AS table, toInt64(count()) AS c
|
||
FROM test.hits
|
||
GROUP BY CounterID
|
||
|
||
UNION ALL
|
||
|
||
SELECT CounterID, 2 AS table, sum(Sign) AS c
|
||
FROM test.visits
|
||
GROUP BY CounterID
|
||
HAVING c > 0
|
||
```
|
||
|
||
Result columns are matched by their index (order inside `SELECT`). If column names do not match, names for the final result are taken from the first query.
|
||
|
||
Type casting is performed for unions. For example, if two queries being combined have the same field with non-`Nullable` and `Nullable` types from a compatible type, the resulting `UNION ALL` has a `Nullable` type field.
|
||
|
||
Queries that are parts of `UNION ALL` can’t be enclosed in round brackets. [ORDER BY](order-by.md) and [LIMIT](limit.md) are applied to separate queries, not to the final result. If you need to apply a conversion to the final result, you can put all the queries with `UNION ALL` in a subquery in the [FROM](from.md) clause.
|
||
|
||
## Limitations
|
||
|
||
Only `UNION ALL` is supported. The regular `UNION` (`UNION DISTINCT`) is not supported. If you need `UNION DISTINCT`, you can write `SELECT DISTINCT` from a subquery containing `UNION ALL`.
|
||
|
||
## Implementation Details
|
||
|
||
Queries that are parts of `UNION ALL` can be run simultaneously, and their results can be mixed together.
|