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
1
vote
3 answers

How to fix inconsistent mysql results?

My MySQL query (SELECT * FROM names WHERE Rate = 75 ORDER BY RAND() LIMIT 1) UNION (SELECT * FROM names WHERE Rate = 75 ORDER BY RAND() LIMIT 1) UNION (SELECT * FROM names WHERE Rate = 35 ORDER BY RAND() LIMIT 1); it should give me something…
Jimboy
  • 47
  • 6
1
vote
1 answer

How to dynamically SELECT from manually partitioned table

Suppose I have table of tenants like so; CREATE TABLE tenants ( name varchar(50) ) And for each tenant, I have a corresponding table called {tenants.name}_entities, so for example for tenant_a I would have the following table. CREATE TABLE…
1
vote
2 answers

selecting enum columns in union all postgres

i have problem figuring out how to set up my query. what i want to do select two tables without relations in one query. after researching i've found out about union all but the problem is in some cases i have type or status columns which is enum and…
Ali Idk
  • 13
  • 3
1
vote
3 answers

SQL MAX of multiple columns and retrieve each row

I want to get the MAX value of close to 20 fields in my database. My issue is that I also want some other fields with all of these MAX values. I'm using PostgreSQL. Here is an example: Match Table definition CREATE TABLE "matches" ( "id" int4…
Kalane
  • 57
  • 2
  • 8
1
vote
3 answers

How to show multiple query results together with column name?

I have four queries with data taken from same table. select sum(unit_sold) as total_unit_sold from tewt where order_priority = 'HH' union all select avg(unit_sold) as average_unit_sold from tewt where order_priority = 'H' union all select…
Eric Gideon
  • 219
  • 1
  • 3
  • 9
1
vote
1 answer

Need to pull by date, that a value appears from different tables

I have tried unions, joins, distinct and minimum functions with some success, but still slightly off. Example of the tables are below ID date 1 01-02-2020 2 02-02-2020 3 04-06-2020 4 01-03-2019 ID …
NewGB
  • 25
  • 4
1
vote
1 answer

How can i join a table with another, then count not empty columns and group them by two other fields?

(I have some issues adding the tables, as they are viewed as code. Have added the tags as code to include it) I have a table with many columns (in example only a…
Levi H.
  • 95
  • 2
  • 13
1
vote
1 answer

Bigquery: run a query multiple times based on different date_trunc and union the results, instead of multiple UNION ALLs

I am looking to pull and union similar aggregations from a core table, but differing on the time period truncation. Eg this is how I would normally do so: with base as ( select event_datetime , event_location , event_uuid from…
emorgsssss
  • 17
  • 4
1
vote
1 answer

Union in big query

Can I ask how to write the correct query for UNION in Big Query. For example, Table A = has some columns which the data type is not string. enter image description here Table B = No such columns in the picture above. Can someone help to write the…
John
  • 17
  • 2
1
vote
3 answers

How to union 2 columns in R

I have two tables and I need to union 2 columns in 1. Each column from a different table. I'd like to create a new table with column z where I will have every value from x & y with dublicates. Can you guys help me with that? id x 1 p 2 e 3 p…
Angelo
  • 21
  • 3
1
vote
2 answers

SELECT FROM TABLES NAME LIKE

I have 1000 tables in a SQL Server database (SSMS) and want to bring selected columns from 1000 tables into 1 new table. I want to do something like this Create a new table with columns a, b, c Identify 1000 tables from which data is to be…
HKJ
  • 15
  • 7
1
vote
1 answer

Row to column transformation from different tables and different rows number (union) in MySQL version 8.0.17 using Pivot

In MySQL 8.0+ using ROW_NUMBER() window function in each table to get a row number and join the tables on that for row to column transformation from different tables (pivot-table) The function working correctly if the 2 tables have the same number…
1
vote
1 answer

SQL union grouped by rows

Assume I have a table like this: col1 col2 col3 col4 commonrow one two null commonrow null null three How to produce a result to look like this: col1 col2 col3 col4 commonrow one two three Thanks
canpoint
  • 817
  • 2
  • 9
  • 19
1
vote
3 answers

How do you join two calculated sql statement?

The statement below unions both queries, but leaves nulls for companies that do not have a calculated value. How can I remove nulls from these two columns? Or is there a better way to combine these two calculated columns into one table.…
YCao
  • 13
  • 3
1
vote
1 answer

Join two count query is not giving right result

I trying to join two query to compare count SELECT count(customer_id) , customer_id FROM `blog_post` group by customer_id and second query is SELECT count(customer_id) FROM `blog_comment` WHERE `is_admin` IS NOT NULL group by customer_id Joined…