How to join 2 tables and have a list of other table's values nested in the first one in PostgreSQL? - sql

Let's assume that we have these 2 tables: person and car
CREATE TABLE person (
id BIGSERIAL NOT NULL PRIMARY KEY,
name VARCHAR NOT NULL
);
CREATE TABLE car (
id BIGSERIAL NOT NULL PRIMARY KEY,
make VARCHAR NOT NULL,
person_id BIGINT NOT NULL REFERENCES person(id)
);
What I try to do is to find all people, find every car and create an array of objects like this one
[
{
"id": "PERSON_ID",
"name": "PERSON_NAME",
"cars": [
{
"id": "CAR_ID",
"model": "MODEL_NAME",
"person_id": "PERSON_ID"
}
]
}
]
I have tried using the AS alias with a JOIN on person table from car table but it didn't work. Is there a way to do this? Thank you!

You may try the following. See a working fiddle:
Schema (PostgreSQL v13)
CREATE TABLE person (
id BIGSERIAL NOT NULL PRIMARY KEY,
name VARCHAR NOT NULL
);
CREATE TABLE car (
id BIGSERIAL NOT NULL PRIMARY KEY,
make VARCHAR NOT NULL,
person_id BIGINT NOT NULL REFERENCES person(id)
);
insert into person(name) values ('tom'),('harry');
insert into car (id,make,person_id) values (1,'ford',1),(2,'audi',1),(3,'nissan',2);
Query #1
SELECT
p.id,
p.name,
array_agg(row_to_json(c)) cars
FROM
person p
INNER JOIN
(SELECT id, make model, person_id FROM car) c ON p.id = c.person_id
GROUP BY
p.id,
p.name;
id
name
cars
1
tom
[{"id":1,"model":"ford","person_id":1},{"id":2,"model":"audi","person_id":1}]
2
harry
[{"id":3,"model":"nissan","person_id":2}]
View on DB Fiddle

If you want the result set as an array in Postgres, you can use:
select p.*, array_agg(c)
from person p join
car c
on c.person_id = p.id
group by p.id;
You can do something similar if you want JSON returned as well.

Related

Return everything from many-to-many relationship with only one query

I'll give an example to better clarify what I want:
Suppose I have the following classes in my programming language:
Class Person(
int id,
string name,
List<Car> cars
);
Class Car(
int id,
string name,
string brand
)
I want to save that in a PostgreSQL database, so I'll have the following tables:
CREATE TABLE person(
id SERIAL,
name TEXT
);
CREATE TABLE car(
id SERIAL,
name TEXT,
brand TEXT
)
CREATE TABLE person_car(
person_id int,
car_id int,
CONSTRAINT fk_person
FOREIGN KEY (person_id)
REFERENCES person(id),
CONSTRAINT fk_car
FOREIGN KEY (car_id)
REFERENCES car(id)
)
Then, I want to select all people with their cars from DB. I can select all people, then for each person, select their cars. But supposing I have 1000 people, I will have to query the DB 1001 times (one to select all people, and one for each person, to get their cars).
Is there an efficient way to bring all people, each with all their cars in a single query, so that I can fill my classes with the correct data without querying the DB a lot of times?
If you want to return a hierarchical dataset, you can use subqueries with COALESCE, for example :
SELECT
p.id
p.name,
COALESCE((SELECT
json_agg(json_build_object(
'id', c.id,
'name', c.name,
'brand', c.brand
))
FROM car AS c
JOIN person_car pc ON c.id = pc.car_id
WHERE pc.person_id = p.id), '[]'::json) AS cars
FROM person AS p;
You are joining person and car to person_car based on their respective ID’s.
SELECT
person.name,
person.id as person_id,
car.name,
car.brand,
car.id as car_id
FROM
person
JOIN
person_car
ON
person.id = person_car.person_id
JOIN
car
ON
car.id = person_car.car_id

How to join two tables with same primary key name but different values

I learnt today about "exclusive entities". I have following ER diagram:
With 2 exclusive entities. When I do a physical data model via power designers creation tool it resolves to
Now I want to join both in one table and display the id and room_name
The structure I want to get is:
room_id | room_name
The room_id's in room and bedroom are different. bedroom has for example the ids 1-10 and kitchen the ids from 11-20.
I have the feeling that I might have a bad design, because the joins I tried don't get me the desired result.
My best guess is to use a natural join like*
SELECT room_id, room_name
FROM bedroom
NATURAL JOIN kitchen;
This returns the correct format but the results are empty.
Furthermore I'm looking to get a table in the format:
room_id | roon_name | bedCount | chairCount
You can do exactly what you requested with a full outer join:
select room_id, room_name, b.bedcount, k.chaircount
from bedroom b full outer join kitchen k using (room_id, room_name)
;
This is almost equivalent to the query you attempted - but you need a natural FULL OUTER join rather than the (inner) natural join you tried. Note, however, that many (most?) practitioners view the natural join syntax with suspicion, for various reasons; the using clause as I demonstrated above seems to be accepted more easily. (Of course, even with a natural join, you might be best served still naming specifically the columns you want in the output.)
Although the more common approach for cases like this is a straightforward union all:
select room_id, room_name, bedcount, cast (null as number) as chaircount
from bedroom
UNION ALL
select room_id, room_name, null , chaircount
from kitchen
;
You can union the two tables together such as:
select room_id, room_name
from bedroom
union
select room_id,room_name
from kitchen
You cannot join them as you have nothing in common between the tables upon which to join the tables. What you need to do is give the tables a common column that they can join upon; such as a room will be in a building so create a buildings table and then each of the rooms should contain a foreign key to the containing building.
I.e.
CREATE TABLE buildings (
id INT
GENERATED ALWAYS AS IDENTITY
CONSTRAINT buildings__id__pk PRIMARY KEY,
name VARCHAR2(20)
NOT NULL
);
CREATE TABLE rooms (
id INT
GENERATED ALWAYS AS IDENTITY
CONSTRAINT rooms__id__pk PRIMARY KEY,
building_id INT
NOT NULL
CONSTRAINT rooms__building_id__fk REFERENCES buildings (id),
room_name VARCHAR2(20)
NOT NULL,
CONSTRAINT rooms__id__rn__u UNIQUE ( id, room_name )
);
CREATE TABLE kitchens (
id INT
CONSTRAINT kitchens__id__pk PRIMARY KEY,
room_name VARCHAR2(20)
GENERATED ALWAYS AS ( 'kitchen' )
NOT NULL,
chairCount INT,
CONSTRAINT kitchens__id__rn__fk
FOREIGN KEY ( id, room_name ) REFERENCES rooms ( id, room_name )
);
CREATE TABLE bedrooms (
id INT
CONSTRAINT bedrooms__id__pk PRIMARY KEY,
room_name VARCHAR2(20)
GENERATED ALWAYS AS ( 'bedroom' )
NOT NULL,
bedCount INT,
CONSTRAINT bedrooms__id__rn__fk
FOREIGN KEY ( id, room_name ) REFERENCES rooms ( id, room_name )
);
Then, if you:
INSERT INTO buildings ( id, name ) VALUES ( DEFAULT, 'Building1' );
INSERT INTO rooms ( id, building_id, room_name ) VALUES ( DEFAULT, 1, 'kitchen' );
INSERT INTO rooms ( id, building_id, room_name ) VALUES ( DEFAULT, 1, 'bedroom' );
INSERT INTO kitchens ( id, chairCount ) VALUES ( 1, 42 );
INSERT INTO bedrooms ( id, bedCount ) VALUES ( 2, 13 );
Then:
SELECT b.id AS building_id,
b.name AS building_name,
rk.id AS kitchen_id,
k.chairCount,
rb.id AS bedroom_id,
br.bedCount
FROM buildings b
LEFT OUTER JOIN rooms rk
ON ( b.id = rk.building_id AND rk.room_name = 'kitchen' )
LEFT OUTER JOIN kitchens k
ON ( rk.id = k.id AND rk.room_name = k.room_name )
LEFT OUTER JOIN rooms rb
ON ( b.id = rb.building_id AND rb.room_name = 'bedroom' )
LEFT OUTER JOIN bedrooms br
ON ( rb.id = br.id AND rb.room_name = br.room_name )
Outputs:
BUILDING_ID | BUILDING_NAME | KITCHEN_ID | CHAIRCOUNT | BEDROOM_ID | BEDCOUNT
----------: | :------------ | ---------: | ---------: | ---------: | -------:
1 | Building1 | 1 | 42 | 2 | 13
db<>fiddle here

postgres: (sub)select and combine optional content into an array

i have the following table structure:
Location----- * Media ----1 Attribute --------* AttributeTranslation
Each Location has n mediaitems attached, containing one optional attribute (text) and n associated translationa for that attribute.
I need to select this data into an array, so that i get for each location the associated medialist for each language.
what i currently do and what i get:
SELECT m.location_id, t.language_id,
array_agg_mult(
ARRAY[ARRAY[m.sortorder::text, m.filename, t.name]] ORDER BY m.sortorder
) as medialist
FROM Media m
LEFT JOIN ATTRIBUTE a ON a.id = m.attribute_id
LEFT JOIN AttributeTranslation t ON a.id = t.attribute_id
WHERE m.location_id = ?
GROUP BY m.location_id, t.language_id
This gives me following result for the given scenario: the current location has 4 images attached, only the first image has an associated attribute containing two translations:
Location_ID Language_ID MEDIALIST
AT_014 1 {{1,'location_image1.jpg','attribute german'}}
AT_014 2 {{1,'location_image1.jpg','attribute english'}}
AT_014 {{2,'location_image2.jpg',null},{3,'location_image3.jpg',null},{4,'location_image4.jpg',null}}
but what i need instead is this:
Location_ID Language_ID MEDIALIST
AT_014 1 {{1,'location_image1.jpg','attribute german'},{2,'location_image2.jpg',null},{3,'location_image3.jpg',null},{4,'location_image4.jpg',null}}
AT_014 2 {{1,'location_image1.jpg','attribute english'},{2,'location_image2.jpg',null},{3,'location_image3.jpg',null},{4,'location_image4.jpg',null}}
those 3 columns are part of a view, so that i can do later:
select * from locationview where location_id = ? and language_id = ?
how can i achieve the desired result here? thanks in advance!
Simplified Table Definitions:
CREATE TABLE LOCATION (
location_id numeric(20) primary key,
description text
);
CREATE TABLE MEDIA (
media_id numeric(20) primary key,
fileName text,
sortorder smallint,
location_id numeric(20) references LOCATION(location_id),
attribute_id numeric(20) references ATTRIBUTE(attribute_id)
);
CREATE TABLE ATTRIBUTE (
attribute_id numeric(20) primary key,
attributetype varchar(100),
);
CREATE TABLE ATTRIBUTETRANSLATION (
translation_id numeric(20),
language_id smallint,
name text,
description text,
attribute_id numeric(20) references ATTRIBUTE(attribute_id)
);
ALTER TABLE ATTRIBUTETRANSLATION add constraint AT_ID primary key(translation_id, language_id)
I am not sure I fully understand your question, but here's an attempt. You could take the output of your query, and match each row that has a language_id with the corresponding rows where language_id is NULL, so that you can then concatenate the medialist arrays. Here's a way to do that by creating an alias of your query with a CTE:
WITH t AS (
SELECT m.location_id, t.language_id,
array_agg(
ARRAY[ARRAY[m.sortorder::text, m.filename, t.name]] ORDER BY m.sortorder
) as medialist
FROM Media m
LEFT JOIN ATTRIBUTE a ON a.attribute_id = m.attribute_id
LEFT JOIN AttributeTranslation t ON a.attribute_id = t.attribute_id
WHERE m.location_id = ?
GROUP BY m.location_id, t.language_id
)
SELECT location_id, t1.language_id, t1.medialist || t2.medialist AS medialist
FROM (SELECT * FROM t WHERE language_id IS NOT NULL) t1
RIGHT OUTER JOIN (SELECT * FROM t WHERE language_id IS NULL) t2 USING (location_id);
I am not sure if this does exactly what you want, but hopefully it will give you some ideas.

SQL issue - type of fkey

I'm using PostgreSQL
What I need
In SELECT query I need to select owner_type (client or domain). If solution does not exist please help me to rework this schema.
Schema (tables)
Albums - id | client_id (fkey) | domain_id (fkey) | name
Clients - id | first_name | last_name
Domains - id | name
Description: Albums owner can be Client or Domain or future other Nodes...
1. CREATE TABLE QUERY
CREATE TABLE albums
(
id BIGSERIAL PRIMARY KEY,
client_id BIGINT,
domain_id BIGINT,
name VARCHAR(255) NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(id),
FOREIGN KEY (domain_id) REFERENCES domains(id),
CHECK ((client_id IS NULL) <> (domain_id IS NULL))
);
2. SELECT QUERY
SELECT albums.id,
albums.name,
COALESCE(c.id, d.id) AS owner_id
FROM albums
LEFT JOIN clients c
ON albums.client_id = c.id
LEFT JOIN domains d
ON albums.domain_id = d.id
Need something like -> if c.id === null -> owner_type = 'Domain'
You would seem to want:
SELECT a.id, a.name,
COALESCE(c.id, d.id) AS owner_id,
(CASE WHEN c.id IS NOT NULL THEN 'client' ELSE 'domain' END) as owner_type
FROM albums a LEFT JOIN
clients c
ON a.client_id = c.id LEFT JOIN
domains d
ON a.domain_id = d.id ;
Do you need two separate columns representing client_id and domain_id for the type of owners? It seems that if you were to add more nodes, you would have to add additional columns.
Could you have an owners table representing all types of owners, and have an owner_id foreign key on the albums table?
I was thinking something like this:
CREATE TABLE albums (
id BIGSERIAL PRIMARY KEY,
owner_id BIGINT,
name VARCHAR(255) NOT NULL,
FOREIGN KEY (owner_id) REFERENCES owners(id)
);
CREATE TABLE owners (
id BIGSERIAL PRIMARY KEY,
type VARCHAR(100) NOT NULL,
name VARCHAR(255) NOT NULL
);
You could then query for albums belonging to clients:
SELECT a.id, a.name, o.name AS owner_name
FROM albums a
JOIN owners o ON o.id = a.owner_id
WHERE o.type = 'Client';
As new nodes (types of owners) are added, you simply need to add them to the owners table without modifying the schema of the albums table.
Hope this helps.

SQL Simple SELECT Query

create table Person(
SSN INT,
Name VARCHAR(20),
primary key(SSN)
);
create table Car(
PlateNr INT,
Model VARCHAR(20),
primary key(PlateNr)
);
create table CarOwner(
SSN INT,
PlateNr INT,
primary key(SSN, PlateNR)
foreign key(SSN) references Person (SSN),
foreign key(PlateNr) references Car (PlateNr)
);
Insert into Person(SSN, Name) VALUES ('123456789','Max');
Insert into Person(SSN, Name) VALUES ('123456787','John');
Insert into Person(SSN, Name) VALUES ('123456788','Tom');
Insert into Car(PlateNr, Model) VALUES ('123ABC','Volvo');
Insert into Car(PlateNr, Model) VALUES ('321CBA','Toyota');
Insert into Car(PlateNr, Model) VALUES ('333AAA','Honda');
Insert into CarOwner(SSN, PlateNr) VALUES ('123456789','123ABC');
Insert into CarOwner(SSN, PlateNr) VALUES ('123456787','333AAA');
The problem I'm having is the SELECTE query I wanna make. I wan't to be able to SELECT everything from the Person and wan't the include the PlateNr of the car he's the owner of, an example:
PERSON
---------------------------------
SSN NAME Car
123456789 Max 123ABC
123456787 John 3338AAA
123456788 Tom
----------------------------------
So, I want to be able to show everything from the Person table and display the content of CarOwner aswell if the person is in fact a CarOwner. What I have so far is: "SELECT * from Person, CarOwner WHERE Person.SSN = CarOwner.SSN;". But this obviously results in only showing the person(s) that are CarOwners.
Hope I explained me well enough, Thanks.
Try this:
SELECT p.*, c.*
FROM Person p
LEFT OUTER JOIN CarOwner co
ON p.SSN = co.SSN
LEFT OUTER JOIN Car c
ON co.PlateNr = c.PlateNr
Show SQLFiddle
P.S. I've changed the type of your primary key PlateNr (in varchar and not in int)
select ssn, name, car
from Person p
LEFT OUTER JOIN CarOwner co
ON p.SSN = co.SSN
LEFT OUTER JOIN Car c
ON co.PlateNr = c.PlateNr