Get distinct ids between multiple columns within the same column in PostgreSQL - sql

So I have a postgresql table that has userids who created a party in one column and an array of userids whom have joined their party.
im wondering if there is any way to return a list of the distinct userids from both the column of their userid and the array, so the distinct list of userids who has either created or joined a party?

If I understand correctly, you can use union:
select created_userid as userid
from t
union -- on purpose to remove duplicates
select joined_userid as userid
from t cross join lateral
unnest(t.joined_userids) joined_userid;

Related

Combine two queries in one (one by one and sorted)

I have a table of models with columns
Both columns are of a varchar type that contains an array of strings.
What I need to achieve here is to get all tags separately, without dups and in specific order. First should be system_tags in alphabetical order, then tags in alphabetical order as well
SELECT
unnest(system_tags) as tag_name
FROM
"models"
left join projects on projects.id = models.project_id
where projects.is_public = true
union
SELECT
unnest(tags)
FROM
"models"
left join projects on projects.id = models.project_id
where projects.is_public = true
I got as far as to get all separated tags without duplications, but can I order them one by one with union?
So I can get result as on the first picture instead as it on the second
Use a union query to combine multiple queries into a single result. Sometimes you might want to list the records from one table or query with those from one or more other tables to form one set of records - a list with all the records from the two or more tables. This is the purpose of a union query in Access.

How to combine two tables with different Primary Keys by multiple columns ideally HASH

I have two tables of world countries Independence Day and I wanted to combine them into one table with a distinct id, but they are using different Primary keys, any suggestions will be appreciated.
Summary of request: How to combine two tables with different Primary Keys but the other fields in common and removing duplicate fields ideally Hash Match and removing duplicates
Expected Results this will include all the unique countries in both tables, please one table may have more countries and we want to make sure we take all the distinct countries from each table. Ideally, the solution will be likely to be of like Hash Match operator in SQL which implements several different logical operations that all use an in-memory hash table for finding matching data. Many thanks in advance
The image of two tables which needs combining.
You seem to want full join:
select a.*, b.* -- select the columns you want
from a full join
b
on a.country = b.country;
If you want to assign a new unique id use row_number():
select row_number() over (order by coalesce(a.country, b.country)) as new_id,
a.*, b.* -- select the columns you want
from a full join
b
on a.country = b.country;

Performing a query on a foreign table but returning only the original table?

Let's say I have a Item and Person table, where the Person 'id' column matches the Item 'person_id' column. I want to retrieve records in the Item table where the owner (Person) of the Item has 'category' 1. Sample structure below:
Item
id
item_name
person_id
Person
id
name
category (can be 1, 2, or 3)
I understand that I can use 'join' to find rows of the Item table where the two ids match, but I cannot use join for my use case. I need my SQL query to return only Item columns, and not Person columns. Basically, how can I construct a query that will query a table using values in another table, while still maintaining the original table structure?
I understand that I can use 'join' to find rows of the Item table where the two ids match, but I cannot use join for my use case. I need my SQL query to return only Item columns, and not Person columns.
This statement is just false. You can select whatever columns you want when joining:
select i.*
from items i join
persons p
on i.person_id = p.id and p.category = 1;
In terms of constructing the query, exists is also a very reasonable approach, but you can definitely use join.
One way is using EXISTS with correlated subquery
SELECT *
FROM Item i
WHERE EXISTS (SELECT 1
FROM Person p
WHERE p.id = i.person_id AND p.category = 1)
You could use IN clause to filter the person table with category 1
SELECT * FROM Item WHERE person_id IN (
SELECT Id FROM Person WHERE category=1)

Replace values in array column with related values from another table

In my database I have a table relations with a column relation_ids containing the IDs of users (user_id). This takes the form of an array with many IDs possible, e.g.:
{111,112,156,4465}
I have another table names containing information on users such as user_id, first_name, last_name etc.
I would like to create an SQL query to return all rows from relations with all columns, but append the array column relation_ids with first_name from the names table substituted for IDs.
Is it possible as some kind of subquery?
Assuming you want to preserve the order in the array - first names listed in the same order as IDs in the original relation_ids.
I suggest an ARRAY constructor over a correlated subquery with unnest() and WITH ORDINALITY, joined to the names table, like:
SELECT r.*
, (ARRAY (
SELECT n.first_name
FROM unnest(r.relation_ids) WITH ORDINALITY AS a(user_id, ord)
JOIN names n ON n.user_id = a.user_id
ORDER BY a.ord
)
) AS first_names
FROM relations r;
This query preserves all rows from relations in any case.
Corner cases to note:
1. A NULL value in relation_ids (for the whole column) is translated to an empty array. (Same as empty array in the source.)
2. NULL elements are silently dropped from the array.
You might want to define desired behavior if those corner cases are possible ...
db<>fiddle here
Related:
LEFT OUTER JOIN on array column with multiple values
PostgreSQL unnest() with element number
Considered a normalized db design:
Can PostgreSQL array be optimized for join?
This will get you all the columns and rows from Relations with first_name appended from the Names table.
Select Relations.relation_ids, Names.user_id, Names.first_name From Relations
Inner Join Names On Relations.user_id=Names.user_id

SQL union or join

You have a list of 2.0 million IDs in Table A. You have another list of 3.5 million IDs in Table B. Some customer IDs show up multiple times in each table and some IDs show up in both tables. Which of the following would you use to create (in one step - no subqueries) a Table C that contains a list of distinct (no duplicates) customer IDs present in either Table A, Table B, or both?
Union, Union ALL, Outer Join, Union Join-?
Union is a SET operation (in SQL and otherwise in mathematics in the field of set theory). A set is a collection of distinct items. Like a list that does not contain any duplicates. A UNION of sets is the combination of all the distinct items in one or more lists.
So in SQL, the UNION function will combine all of its parameters and return a SET of unique values in the combined list.