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
16
votes
1 answer

Mysql UNION and GROUP BY

I have 2 tables I need to add together based on a date and 2 values. This gives me the list of all information - fine. $query = (SELECT date, debit, credit , note FROM proj3_cash ) UNION (SELECT settle, purch, sale, issue FROM…
user1682381
  • 161
  • 1
  • 1
  • 3
15
votes
4 answers

How do I write a UNION chain with ActiveRelation?

I need to be able to chain an arbitrary number of sub-selects with UNION using ActiveRelation. I'm a little confused by the ARel implementation of this, since it seems to assume UNION is a binary operation. However: ( select_statement_a ) UNION (…
Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
15
votes
2 answers

Selecting across multiple tables with UNION

I reworked my database from one user table to multiple user tables (divided per role): tblStudents, tblTeachers, tblAdmin When logging in, I didn't want to run three queries to check if the user exists somewhere in my DB. So what I did was put…
Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
15
votes
1 answer

Avro Schema. How to set type to "record" and "null" at once

I need to mix "record" type with null type in Schema. "name":"specShape", "type":{ "type":"record", "name":"noSpecShape", "fields":[ { "name":"bpSsc", …
Nadir Novruzov
  • 467
  • 1
  • 6
  • 16
15
votes
3 answers

Order by clause with Union in Sql Server

I want List of party names with 1st option as 'All' from database. but i won't insert 'All' to Database, needs only retrieve time. so, I wrote this query. Select 0 PartyId, 'All' Name Union select PartyId, Name from PartyMst This is my Result 0 …
Dhaval Ptl
  • 494
  • 1
  • 5
  • 18
14
votes
3 answers

reverse operation to "Union and find"

We know there is "Union and find" for disjoint sets. http://en.wikipedia.org/wiki/Union_find But how to do reverse operation ? Consider a set with N nodes connected with E edges( which is in fact a graph ). And at each step we want to delete some…
Krzysztof Lewko
  • 982
  • 7
  • 24
14
votes
5 answers

Creating MySQL View using UNION

I am trying to create a view for the following query. SELECT DISTINCT products.pid AS id, products.pname AS name, products.p_desc AS description, products.p_loc AS location, products.p_uid AS userid, …
aLL0i
14
votes
1 answer

My coproduct encoding is ambiguous

This question has come up a few times recently, so I'm FAQ-ing it here. Suppose I've got some case classes like this: import io.circe._, io.circe.generic.semiauto._ object model { case class A(a: String) case class B(a: String, i: Int) case…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
14
votes
4 answers

Getting a distinct value across 2 union sql server tables

I'm trying to get all distinct values across 2 tables using a union. The idea is to get a count of all unique values in the columnA column without repeats so that I can get a summation of all columns that contain a unique columnA. This is what I…
rockit
  • 3,708
  • 7
  • 26
  • 36
14
votes
2 answers

Why is UNION faster than an OR statement

I have a problem where I need to find records that either have a measurement that matches a value, or do not have that measurement at all. I solved that problem with three or four different approaches, using JOINs, using NOT IN and using NOT EXISTS.…
Derk Arts
  • 3,432
  • 2
  • 18
  • 36
14
votes
5 answers

Same number of columns for Union Operation

I was going through union and union all logic and trying examples. What has puzzled me is why is it necessary to have same number of columns in both the tables to perform a union or union all operation? Forgive me if my question's silly, but i…
Sparky
  • 743
  • 6
  • 15
  • 28
13
votes
5 answers

Why are UNION queries so slow in MySQL?

When I optimize my 2 single queries to run in less than 0.02 seconds and then UNION them the resulting query takes over 1 second to run. Also, a UNION ALL takes longer than a UNION DISTINCT. I would assume allowing duplicates would make the query…
Greg
  • 7,233
  • 12
  • 42
  • 53
13
votes
1 answer

How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues

I am trying to combine (union or concat) two lists/collection into one. The two lists have a common base class. e.g. I've tried this: IQueryable contractItems = myRepository.RetrieveContractItems(); …
Raymond
  • 3,382
  • 5
  • 43
  • 67
13
votes
3 answers

MySQL: looking to SUM these UNIONs together

OK, my head hurts...! This beautiful MySQL query: (SELECT mtwitterfollowers AS twitfollow FROM `media` WHERE media.id=1) UNION (SELECT SUM(twitterfollowers) AS twitfollow FROM people LEFT JOIN peoplejoin ON peoplejoin.people_id = people.id LEFT JOIN…
jamescridland
  • 714
  • 2
  • 5
  • 24
13
votes
4 answers

Conditional UNION in stored procedure

Bonjour! So, in a stored procedure I would like to do a conditional union decided by a parameter. How can I do that? Here is my "doesn't work" procedure : SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE…
bAN
  • 13,375
  • 16
  • 60
  • 93