Questions tagged [union-all]

"UNION ALL" is a keyword in SQL that is used for combining the results of multiple SELECTs. The related tag is "UNION". "UNION ALL" combines the results without checking for Uniqueness. "UNION" combines the results eliminating duplicates.

UNION ALL is a keyword in SQL that is used for combining the results of multiple SELECTs. The related tag is UNION (). UNION ALL combines the results without checking for uniqueness. UNION combines the results eliminating duplicates. The type and order of the fields in the two SELECTs should be the same.

775 questions
4
votes
2 answers

Using Union All and Order By in MySQL

I've 2 tables: create table advertised_products(id int,title varchar(99),timestamp timestamp); insert advertised_products select 1,'t1',curdate(); create table wanted_products(id int,title varchar(99),timestamp timestamp); insert wanted_products…
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
4
votes
0 answers

How to force push predicate through UNION ALL inside a view?

I have a performance problem on a UNION ALL view. The problem can be solved by rewriting the view in two separate views but that kind of defeats the purpose of creating a view. Here is a simple test case (Oracle 11.2.0.3.0). The real queries use…
jva
  • 2,797
  • 1
  • 26
  • 41
3
votes
1 answer

Insert multiple values with the same foreign key

I have two tables that reference each other: CREATE TABLE Room room_id INTEGER PRIMARY KEY, room_name TEXT UNIQUE NOT NULL; CREATE TABLE Item item_id INTEGER PRIMARY KEY, room_id INTEGER, item_name TEXT, FOREIGN KEY…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
3
votes
2 answers

Is this a well documented pattern?

I am trying to find if the below is a well documented pattern (or anti-pattern for that matter) for reducing application latency. I have tried this technique and on face this looks to be saving me some 20% in latency. I would like to know if there…
rajeshnair
  • 1,587
  • 16
  • 32
3
votes
2 answers

Create a UNION ALL query when the columns are in different order

I have 600 tables to perform a UNION ALL query on. Unfortunately the order of the columns in each table varies, however they will always have the same name - example: Table 1 Item, Cost, ID, Code, Location Table 2 Cost, Id, Code, Location,…
James2086
  • 201
  • 2
  • 12
3
votes
3 answers

Merge two SELECT queries with different ORDER BY

I have a Story table, and I need the following query: - first 6 rows sorted by distance (I calculate it) - next rows are sorted by TIME property descending declare @profileID int set @profileID = 6 declare @longitude float set @longitude =…
Wasyster
  • 2,279
  • 4
  • 26
  • 58
3
votes
2 answers

Merging 3 tables/queries using MS Access Union Query

I have built a MySQL database to store bill payments. Everyone in my office has MS Access, so I am building a front-end database reporting tool using MS Access and linking to the MySQL tables on backend. I have created some Access queries that…
RyanKDalton
  • 1,271
  • 3
  • 14
  • 30
3
votes
1 answer

MySQL UNION ALL performance tuning

SELECT `col1` FROM `tbl1`; -- takes 0.0022s SELECT `col1` FROM `tbl2`; -- takes 0.0017s SELECT `col1` FROM `tbl1` UNION ALL (SELECT `col1` FROM `tbl2`); -- takes 0.1100s Why UNION ALL working slow? Any other alternative of using UNION ALL?
3
votes
1 answer

Joining sql views in oracle sql

How can I join views using sql? I'm using Oracle at the moment? Sql view 1 CREATE VIEW florence_staff AS SELECT * FROM staff WHERE libname ='florence' Sql view 2 CREATE VIEW alexandria_staff AS SELECT * FROM staff WHERE libname ='alexandria' I'm…
getaway
  • 8,792
  • 22
  • 64
  • 94
3
votes
2 answers

Dynamic UNION ALL query in Postgres

We are using a Postgres / PostGis connection to get data that is published via a geoserver. The Query looks like this at the moment: SELECT row_number() over (ORDER BY a.ogc_fid) AS qid, a.wkb_geometry AS geometry FROM ( SELECT * FROM test …
Bernd Loigge
  • 1,647
  • 1
  • 14
  • 17
3
votes
1 answer

Laravel UNION ALL not working with where statement

I want to get two queues into one. $buildings_queue=IngameBuildingQueue::where(DB::raw('UNIX_TIMESTAMP(`start_time` + `duration`)'),'<=',time()); $recruit_queue=IngameRecruitQueue::where(DB::raw('UNIX_TIMESTAMP(`start_time` +…
user5166954
3
votes
5 answers

Alternatives to UNION ALL to get aggregate data

What are alternatives to UNION ALL for joining many tables to see aggregated data from many source systems? I am looking for solution beyond T-SQL. Suppose we have 3 source tables and we want to join them by id: TableA id Adata 1 8383 2 2707 3 …
Przemyslaw Remin
  • 6,276
  • 25
  • 113
  • 191
3
votes
3 answers

T-SQL [UNION ALL] removing records from query result

Have a simple UNION ALL query marrying the results of two queries. The first query, run independently, returns 1208 records and the second 14. I would expect a properly syntaxed UNION ALL to return 1222 records but mine falls to 896. Makes zero…
3
votes
4 answers

Union all, combining query results

I have following MySQL queries: SELECT * FROM bookings WHERE record_id = 7 AND status = 'available' AND startdate >= '2015-05-02' AND startdate <= '2015-05-09' UNION ALL SELECT * FROM bookings WHERE record_id = 7 AND status = 'available' AND…
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
3
votes
2 answers

Union on two tables with a where clause in the one

Currently I have 2 tables, both of the tables have the same structure and are going to be used in a web application. the two tables are production and temp. The temp table contains one additional column called [signed up]. Currently I generate a…
Lostdrifter
  • 35
  • 1
  • 1
  • 3