Questions tagged [union]
Combines two or more query results. UNION removes duplicates; UNION ALL retains all rows.
238 questions
0 votes
0 answers
50 views
How to use the same subquery in both queries for UNION
I have an SQL query with a subquery, like this: SELECT field1, json_agg(json_object('key', field2, 'total', "total") order by field2) as "my data" FROM ( SELECT field1, ...
3 votes
2 answers
185 views
Select row with max value for multiple columns
I have a table with speeds at different time intervals: CREATE TABLE speeds ( result_id uuid NULL, t10 float4 NULL, t30 float4 NULL, t60 float4 NULL, t120 float4 NULL); And some ...
17 votes
1 answer
2k views
Union does not always eliminate duplicates
I have the following query and expect that as a result I will have a list of IDs without duplicates. But sometimes it produces duplicates (1-2 on 4 million rows). Why can it happen? I run it with the ...
2 votes
1 answer
158 views
UNION Two Queries and then Aggregate
I have two different queries that I need to UNION together and then aggregate each rate type group together into a final table. For each rate group (Rate_001mo, Rate_003mo, etc.) SELECT EffectiveDate,...
8 votes
1 answer
294 views
Order and nature of columns in select list of sorted UNION query affects performance
I am observing significant performance impact of select lists in sorted UNION queries. The general form of UNION queries I am working with is: SELECT * FROM ( SELECT <select_list> FROM <...
0 votes
2 answers
121 views
ORDER column by numeric order not alphabet
i am stuck with an exercise. using WideWorldImporters DataBase I need to show my results this way: but no matter what I do in order to get to this exact solution I can't, because the month column is ...
0 votes
2 answers
117 views
Can This Code Be Exploited for SQL Injection with Significant Impact?
I’m currently debating with a colleague whether the following (pseudo) code is vulnerable to SQL injection (SQL Server): database.BeginTransaction(); String userId = dto.UserId; String firstQuery = &...
0 votes
0 answers
287 views
An alternative to PostgreSQL union?
In the following example variables in single quotes '' are arguments passed to the PostgreSQL query. If 'tag_args' is an empty array, the first row of result table is a row of three nulls and I use ...
1 vote
2 answers
2k views
Why is Postgres so slow to order those 200 rows that are already ordered?
I have two relatively complex SQL queries which I join using a UNION ALL. Each individual query is fast and returns instantly. The problem is that once joined together they perform terribly bad, and ...
2 votes
1 answer
286 views
Can we exchange logical disjunction for UNION ALL?
I saw in that somebody exchange below code: SELECT PK1 , PK2 , PK3 , PK4 , C , B , SUM(NUMERIC_1) , SUM(NUMERIC_2) , MAX(NUMERIC_3) , SUM(NUMERIC_4) FROM ...
0 votes
0 answers
234 views
In PostgreSQL, UNION ALL is slow once three or more subqueries are merged
These tests were ran on PostgreSQL v15.2 I'm using UNION ALL to combine the results of various queries. The separate queries perform well: explain analyze (with params as (select foo, data->>'...
0 votes
1 answer
111 views
May I expect PostgreSQL combines row sources in union all in the same order they are defined in query?
PostgreSQL seems to fail recognising the sorting is performed by constant and and each constant unambiguously defines corresponding row source, and as a result I observe it computes all row sources ...
0 votes
1 answer
112 views
Compose a complex query in Postgresql
I have a huge table like following: CREATE TABLE public.huge_table ( sampl_day date NOT NULL, tick_time timestamp(6) with time zone NOT NULL, crit_feat integer NOT NULL, --- --- ...
1 vote
3 answers
5k views
CTE with UNION ALL not working as expected
The query below seems simple and straightforward, yet it produces unexpected results. CREATE TABLE #NUMBERS ( N BIGINT ); INSERT INTO #NUMBERS VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9) ;...
6 votes
1 answer
725 views
How to reduce query size with many repeated UNION subqueries?
I use Postgres 13 and have a table defined with the following DDL: CREATE TABLE item_codes ( code bytea NOT NULL, item_id bytea NOT NULL, time ...