Finding homonyms in postgresql - postgresql

I have this table
CREATE TABLE Repondant
matricule CHAR(8) NOT NULL, -- Roll
name VARCHAR(64) NOT NULL,
roll VARCHAR(64) NOT NULL,
email VARCHAR(80) NOT NULL,
CONSTRAINT Repondant_cc0 PRIMARY KEY (matricule),
CONSTRAINT Repondant_cc1 UNIQUE (courriel),
CONSTRAINT Repondant_matricule CHECK (matricule SIMILAR TO '[0-9]{8}'),
CONSTRAINT Repondant_nom CHECK (LENGTH(nom) > 0),
CONSTRAINT Repondant_prenom CHECK (LENGTH(prenom) > 0),
CONSTRAINT Repondant_courriel CHECK (
LOWER(courriel) SIMILAR TO
'[^][()<>:;#\,."[:space:][:cntrl:]]+(\.[^][()<>:;#\,."[:space:][:cntrl:]]+)*\#hotmail.com'
)
);
I want to make a request that gives me the name,lastname,email and roll for all the homonyms in the table(same name and same firstname)
I tried
(SELECT name,lastname,role,email
FROM Repondant
Where name = (SELECT name FROM Repondant GROUP BY nom HAVING COUNT(*)>1) AND
lastname = (SELECT lastname FROM Repondant GROUP BY prenom HAVING COUNT(*)>1)
Problem is, it doesnt give the homonyms, it only gives the persons that have their name and their Lastname at least 1 time in the table, but not alway together.
How should I find the homonyms ?

You can build a array to check the occurrence of more than a value.
For example this is a variant with a subquery:
SELECT * from repondant
WHERE (name,lastname) in
(
SELECT name,lastname
FROM repondant
GROUP BY name, lastname
HAVING count(array[name,lastname]) > 1
);

Related

I can't create this PostgreSQL Query: Grouping by a different condition to the "order by" condition

I need to order my query in a different way i need to group my tables. I need to count how many men are in every department, but organize the query by quantity of people (Not only men, but also women) in every department, in descending way.
This is the diagram and the code of the tables:
Relational model of the tables
CREATE SCHEMA Academico;
CREATE TABLE Academico.PAIS(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_PAIS PRIMARY KEY (ID));
CREATE TABLE Academico.DEPARTAMENTO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
CODIGO int NOT NULL,
DESCRIPCION varchar(120) NULL,
IDPAIS int NOT NULL,
CONSTRAINT PK_DEPARTAMENTO PRIMARY KEY (ID));
CREATE TABLE Academico.CIUDAD(
ID int NOT NULL,
NOMBRE varchar(255) NOT NULL,
CODIGO int NOT NULL,
DESCRIPCION varchar(120) NULL,
IDDEPARTAMENTO int NOT NULL,
CONSTRAINT PK_CIUDAD PRIMARY KEY (ID));
ALTER TABLE Academico.DEPARTAMENTO
ADD CONSTRAINT FK_DEPARTAMENTO_PAIS FOREIGN KEY(IDPAIS)
REFERENCES Academico.PAIS (ID)
on delete restrict on update restrict;
ALTER TABLE Academico.CIUDAD
ADD CONSTRAINT FK_CIUDAD_DEPARTAMENTO FOREIGN KEY(IDDEPARTAMENTO)
REFERENCES Academico.DEPARTAMENTO (ID)
on delete restrict on update restrict;
CREATE TABLE Academico.SEXO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_SEXO PRIMARY KEY (ID));
CREATE TABLE Academico.TIPODOCUMENTO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_TIPODOCUMENTO PRIMARY KEY (ID));
CREATE TABLE Academico.PERSONA(
ID int NOT NULL,
NOMBRE varchar(10) NOT NULL,
APELLIDO varchar(30) NOT NULL,
IDSEXO int NOT NULL REFERENCES Academico.SEXO(id),
IDCIUDAD int NOT NULL REFERENCES Academico.CIUDAD(id),
DOCUMENTO varchar(50) NOT NULL,
IDTIPODOCUMENTO int NOT NULL REFERENCES Academico.TIPODOCUMENTO(id),
FECHANACIMIENTO date NULL CHECK (FECHANACIMIENTO > '1900-01-01'),
FEvarcharEGISTRO date NOT NULL DEFAULT Now() ,
email varchar (355) UNIQUE NOT NULL,
PROFESION varchar(12) NULL,
PERFIL varchar(120) NULL,
CONSTRAINT PK_PERSONA PRIMARY KEY
(ID) );
I tried this two querys that give me the expected results but in a separated way:
select
d.nombre as _departamento, s.nombre as sex, count(1) as total_sexo
from
academico.persona p, academico.sexo s,
academico.ciudad c, academico.departamento d
where
p.idsexo = s.id
and p.idciudad = c.id
and c.iddepartamento = d.id
and upper( s.nombre ) = 'MASCULINO'
group by
d.id,
s.id
order by
d.nombre
-- =======================================================
-- I don't know how to "merge" these two into one query
-- =======================================================
select
d.nombre as _departamento, count(1) as total_gente
from
academico.persona p, academico.ciudad c,
academico.departamento d, academico.sexo s
where
p.idciudad = c.id
and c.iddepartamento = d.id
and p.idsexo = s.id
group by
d.id
order by
total_gente desc
;
I need to get those results with only one query
This is the perfect use for the FILTER (WHERE...) construct.
...
count(1) as total_gente,
count(1) filter (where upper( s.nombre ) = 'MASCULINO') as total_masculino
...
And then take the upper( s.nombre ) = 'MASCULINO' out of the main where clause.

I get error as "duplicate key value violates unique constraint "

I'm working on a data warehouse. I have 4 table on public schema they are customer, product, addressee and orders
Then I created this tables on my olap schema
CREATE TABLE olap.time
(
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees
(
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers
(
idcustomer varchar(10) PRIMARY KEY ,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
CREATE TABLE olap.fact
(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer) REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES olap.time(idtime)
);
After the creating tables I run these queries
INSERT INTO olap.time (year, month, week, day)
SELECT date_part('year', date), date_part('month', date), date_part('week', date), date_part('day', date)
FROM public.orders
GROUP BY public.orders.date
ORDER BY public.orders.date;
INSERT INTO olap.addressees(idaddressee, name, zip, address)
SELECT idaddressee, name, zip, address
FROM public.addressee;
INSERT INTO olap.customers (idcustomer, name, city, zip, address, email, phone, regon)
SELECT idcustomer, name, city, zip, address, email, phone, regon
FROM public.customer;
And then I try to do these set of query
INSERT INTO olap.fact (idtime, idaddressee, idcustomer, idfact, price)
SELECT olap.time.idtime, olap.addressees.idaddressee, olap.customers.idcustomer, COUNT(*), public.orders.price
FROM (((public.orders
INNER JOIN olap.time ON (date_part('year', public.orders.date) = olap.time.year AND date_part('month', public.orders.date) = olap.time.month AND date_part('week', public.orders.date) = olap.time.week) AND date_part('day', public.orders.date) = olap.time.day)
INNER JOIN olap.addressees ON public.orders.idaddressee = olap.addressees.idaddressee)
INNER JOIN olap.customers ON public.orders.idcustomer = olap.customers.idcustomer)
GROUP BY olap.time.idtime, olap.addressees.idaddressee, olap.customers.idcustomer, public.orders.price;
After running last set of queries I got error
ERROR: syntax error at or near "duplicate"
LINE 1: duplicate key value violates unique constraint"
What can the problem be? Thanks in advance

how to retrieve data from multiple tables (postgresql)

I have 4 different tables that are linked to each other in the following way (I only kept the essential columns in each table to emphasise the relationships between them):
create TABLE public.country (
country_code varchar(2) NOT NULL PRIMARY KEY,
country_name text NOT NULL,
);
create table public.address
(
id integer generated always as identity primary key,
country_code text not null,
CONSTRAINT FK_address_2 FOREIGN KEY (country_code) REFERENCES public.country (country_code)
);
create table public.client_order
(
id integer generated always as identity primary key,
address_id integer null,
CONSTRAINT FK_client_order_1 FOREIGN KEY (address_id) REFERENCES public.address (id)
);
create table public.client_order_line
(
id integer generated always as identity primary key,
client_order_id integer not null,
product_id integer not null,
client_order_status_id integer not null default 0,
quantity integer not null,
CONSTRAINT FK_client_order_line_0 FOREIGN KEY (client_order_id) REFERENCES public.client_order (id)
);
I want to get the data in the following way: for each client order line to show the product_id, quantity and country_name(corresponding to that client order line).
I tried this so far:
SELECT country_name FROM public.country WHERE country_code = (
SELECT country_code FROM public.address WHERE id = (
SELECT address_id FROM public.client_order WHERE id= 5
)
)
to get the country name given a client_order_id from client_order_line table. I don't know how to change this to get all the information mentioned above, from client_order_line table which looks like this:
id client_order_id. product_id. status. quantity
1 1 122 0 1000
2 2 122 0 3000
3 2 125 0 3000
4 3 445 0 2000
Thanks a lot!
You need a few join-s.
select col.client_order_id,
col.product_id,
col.client_order_status_id as status,
col.quantity,
c.country_name
from client_order_line col
left join client_order co on col.client_order_id = co.id
left join address a on co.address_id = a.id
left join country c on a.country_code = c.country_code
order by col.client_order_id;
Alternatively you can use your select query as a scalar subquery expression.

Modify existing query

I have two tables
create table jobs (
id varchar unique primary key,
account_email varchar not null,
active boolean not null default true,
enabled boolean not null default false,
name varchar (50) not null,
...
);
create table job_tags (
job_id varchar not null,
tag varchar(50) not null,
foreign key (job_id) references jobs(id) on delete cascade,
unique (job_id, tag)
);
And this sql query to get job SELECT * FROM jobs INNER JOIN job_categories ON (jobs.category_id=job_categories.category_id) WHERE jobs.id=$1
Since I have little experience I perform one more query in order to load job_tags. Is it possible to create only one? I work with golang sqlx, thanks
Yes, you almost got it:
SELECT * FROM jobs
INNER JOIN job_categories ON (jobs.category_id=job_categories.category_id)
INNER JOIN job_tags ON (jobs.id = job_tags.job_id)
WHERE jobs.id=$1

How to group by an attribute and order by date

I have two tables:
Medics
CREATE TABLE "medic" (
"id" BIGINT NOT NULL,
"name" CHARACTER VARYING(255) NOT NULL,
PRIMARY KEY ("id")
);
Comments
CREATE TABLE IF NOT EXISTS "comment" (
"id" BIGINT NOT NULL,
"medic_id" BIGINT NOT NULL,
"comment" CHARACTER VARYING(1024) NOT NULL,
"created_at" TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT now(),
CONSTRAINT pk_comment PRIMARY KEY (id),
CONSTRAINT fk_comment_medic FOREIGN KEY (medic_id)
REFERENCES medic(id) ON UPDATE NO ACTION ON DELETE NO ACTION
);
Now I want to get medic_id, name, comments_count and all ordered by created_at
Here's what I've tried so far:
SELECT m.id, m.name, COUNT(c.id)
FROM COMMENT AS c
JOIN medic AS m ON m.id = c.medic_id
GROUP BY m.id, m.name, c.created_at
ORDER BY c.created_at DESC
But obviously this can't work because it makes no sense to group by date although I have to do it when I want to order by date.
Another appraoch was to work with window functions. Particularly rank() over (partition by m.id order by c.created_at desc). But in this case I lose the ordering over all records.
Here's some SQLFiddle.
I am using Postgres 9.3
I'm guessing you want to order by the most recent comment date:
SELECT m.id, m.name, COUNT(c.id)
FROM COMMENT c JOIN
medic m
ON m.id = c.medic_id
GROUP BY m.id, m.name
ORDER BY MAX(c.created_at) DESC;