Questions tagged [union]

Use this tag only for questions about UNION, a keyword of the SQL language for combining the results of multiple SQL queries. Use [union-all] for the UNION-ALL keyword. Use the tag [unions] for unions in C, C++, and similar languages.

UNION is a keyword of SQL for combining the results of multiple SQL queries. The results are combined and duplicate rows are eliminated (similar to DISTINCT). If UNION ALL is used, the rows are combined but duplicates are not removed.

Use the tag for unions in C, C++, and similar languages. Use for the UNION-ALL keyword.

Reference

5266 questions
19
votes
4 answers

How to write UNION in Doctrine 2.0

How to write this SQL query in Doctrine 2.0 (and fetch results)? (SELECT 'group' AS type, CONCAT(u.firstname, " ", u.surname) as fullname, g.name AS subject, user_id, who_id, group_id AS subject_id, created FROM…
Iškuda
  • 635
  • 1
  • 7
  • 13
19
votes
6 answers

Union of intervals

I've got a class representing an interval. This class has two properties "start" and "end" of a comparable type. Now I'm searching for an efficient algorithm to take the union of a set of such intervals. Thanks in advance.
drvj
  • 355
  • 1
  • 4
  • 9
18
votes
5 answers

select max, min values from two tables

I have two tables. Differ in that an archive is a table and the other holds the current record. These are the tables recording sales in the company. In both we have among other fields: id, name, price of sale. I need to select from both tables, the…
BKl
  • 415
  • 1
  • 8
  • 23
18
votes
5 answers

Typescript: understanding union and Intersection types

I am trying to get an intuition about Union and Intersection types in typescript, but I can't figure out this case: Playground Link interface A { a: number; } interface B{ b: boolean; } type UnionCombinedType = A | B; type…
niryo
  • 1,275
  • 4
  • 15
  • 26
17
votes
5 answers

How can I treat a UNION query as a sub query

I have a set of tables that are logically one table split into pieces for performance reasons. I need to write a query that effectively joins all the tables together so I use a single where clause of the result. I have successfully used a UNION on…
Michael Rutherfurd
  • 13,815
  • 5
  • 29
  • 40
17
votes
3 answers

UNION types "..." and "..." cannot be matched

When I run below query I am getting this error UNION types text and bigint cannot be matched SELECT 1 AS STEP , '' AS ProviderName , '' AS Procedurecode , Claimid , Patient_First_Name , Patient_Last_Name , DOS ,…
user4287146
  • 179
  • 1
  • 1
  • 7
17
votes
3 answers

How can I add a column to this union result?

I have this query (which I removed some keys from for brevity's sake): SELECT id as in_id, out_id, recipient, sender, read_flag FROM received WHERE recipient=1 UNION ALL SELECT in_id, id AS out_id, recipient, sender, read_flag FROM sent…
delete me
17
votes
5 answers

ORDER BY with a UNION of disparate datasets (T-SQL)

I have a query that UNION's two somewhat similar datasets, but they both have some columns that are not present in the other (i.e., the columns have NULL values in the resulting UNION.) The problem is, I need to ORDER the resulting data using those…
NateJ
  • 1,935
  • 1
  • 25
  • 34
16
votes
2 answers

How to union two data tables and order the result

Q: If I have two DataTables like this : Dt1(emp_num,emp_name,type) Dt2(emp_num,emp_name,type) I wanna to Union them and order the result by emp_name.
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
16
votes
5 answers

Combining NSArrays Through Intersection and Union

I have two NSArrays A and B that share some common elements, e.g. A: 1,2,3,4,5 B: 4,5,6,7 I would like to create a new NSArray consisting of the contents common between the two NSArrays joined with the contents of the second NSArray while…
Mat Kelly
  • 2,327
  • 7
  • 29
  • 51
16
votes
6 answers

The used SELECT statements have a different number of columns

For examples I don't know how many rows in each table are and I try to do like this: SELECT * FROM members UNION SELECT * FROM inventory What can I put to the second SELECT instead of * to remove this error without adding NULL's?
good_evening
  • 21,085
  • 65
  • 193
  • 298
16
votes
3 answers

Why would I want .union over .unionAll in Spark for SchemaRDDs?

I'm trying to wrap my head around these two functions in the Spark SQL documentation– def union(other: RDD[Row]): RDD[Row] Return the union of this RDD and another one. def unionAll(otherPlan: SchemaRDD): SchemaRDD Combines the tuples of two RDDs…
duber
  • 2,769
  • 4
  • 24
  • 32
16
votes
4 answers

Union of two arrays in PostgreSQL without unnesting

I have two arrays in PostgreSQL that I need to union. For example: {1,2,3} union {1,4,5} would return {1,2,3,4,5} Using the concatenate (||) operator would not remove duplicate entries, i.e. it returns {1,2,3,1,4,5} I found one solution on the web,…
MrGlass
  • 9,094
  • 17
  • 64
  • 89
16
votes
3 answers

HiveQL UNION ALL

I have table_A: id var1 var2 1 a b 2 c d Table_B: id var1 var2 3 e f 4 g h All I want is table, combined: id var1 var2 1 a b 2 c d 3 e f 4 g h This is my .hql: CREATE TABLE combined AS SELECT all.id,…
dum_dum_dummy
  • 161
  • 1
  • 1
  • 3
16
votes
2 answers

MySQL true row merge... not just a union

What is the mysql I need to achieve the result below given these 2 tables: table1: +----+-------+ | id | name | +----+-------+ | 1 | alan | | 2 | bob | | 3 | dave | +----+-------+ table2: +----+---------+ | id | state …
panofish
  • 7,578
  • 13
  • 55
  • 96