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

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.

Related

What is wrong with this Query ? PostgreSQL

I want to create 3 tables and join them with foreign keys. Unfortunately, it doesn't work and I have no idea where is a mistake.
CREATE TABLE Students (
Student_Id SERIAL PRIMARY KEY,
Name VARCHAR (20) NOT NULL,
Surname VARCHAR (30) NOT NULL,
Date_of_Birth DATE NOT NULL,
Phone INT NOT NULL UNIQUE,
Email VARCHAR(225) NOT NULL UNIQUE,
Course_Id INT
FOREIGN KEY (Course_Id) REFERENCES Course (Course_Id)
);
CREATE TABLE Course (
Course_Id SERIAL PRIMARY KEY,
Student_Id INT NOT NULL,
Teacher_Id INT NOT NULL,
Category VARCHAR (30) NOT NULL,
FOREIGN KEY (Student_Id) REFERENCES Students (Student_Id)
);
CREATE TABLE Teachers (
Teacher_Id SERIAL PRIMARY KEY,
Name VARCHAR (20) NOT NULL,
Surname VARCHAR (30) NOT NULL,
Phone INT NOT NULL UNIQUE,
Salary INT NOT NULL,
Course_Id INT NOT NULL,
FOREIGN KEY (Teacher_Id) REFERENCES Course (Teacher_Id)
);
I should create a Foreign Key to join all three tables.
I get this error every time: relation "course" does not exist
I can't find where is the mistake. Please help.
It appears like you are attempting to crate a many-to-many (M:M) between Students and Teachers with Course as the resolution table. You are close, however, your definition sets up bi-directional relationships. This is normally not necessary. Define Students and Teachers without the FK to Course. Then define Course with a FK to each.
create table students (
student_id serial primary key
, name varchar (20) not null
, surname varchar (30) not null
, date_of_birth date not null
, phone int not null unique
, email varchar(225) not null unique
);
create table teachers (
teacher_id serial primary key
, name varchar (20) not null
, surname varchar (30) not null
, phone int not null unique
, salary int not null
);
create table course (
course_id serial primary key
, student_id int not null
, teacher_id int not null
, category varchar (30) not null
, foreign key (student_id) references students (student_id)
, foreign key (teacher_id) references teachers (teacher_id)
, unique (student_id, teacher_id)
);
If you must define bi-directional FK in Students and Teachers then create the tables without the FK then use alter table after Course is defined to add the FK and make them DEFERRABLE INITIALLY DEFERRED. Necessary for eventual Inserts.
alter table teachers add column course_id int references course(course_id) deferrable initially deferred;
alter table students add column course_id int references course(course_id) deferrable initially deferred;

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

TypeORM: many to many: Get all teams from userID

I have two table: User and Team.
They are in a many-to-many relation.
I would like to get all teams of a specific user.
Seams really simple but I can't find the answer anywhere..
Do you know how can I do ?
On the many-to-many relation must be 3rd table. This table contained user_id and team_id connections.
For example:
CREATE TABLE user (
id serial4 NOT NULL,
first_name varchar(200) NOT NULL,
last_name varchar(200) NULL,
CONSTRAINT user_pk PRIMARY KEY (id)
);
CREATE TABLE team (
id serial4 NOT NULL,
team_name varchar NOT NULL,
team_about text NULL,
CONSTRAINT team_pk PRIMARY KEY (id)
);
CREATE TABLE user_team (
id serial4 NOT NULL,
user_id int4 NOT NULL,
team_id int4 NOT NULL,
CONSTRAINT user_team_pk PRIMARY KEY (id)
);
-- filter and select team by user_id
select t.* from examples.team t
inner join examples.user_team usrt on usrt.team_id = t.id
where usrt.user_id = 2;

postgresql display products that were ordered

i havŠµ tables like this and i need to display products that were ordered
create table customer (
id int primary key,
first_name varchar(100) not null,
last_name varchar(100) not null,
city varchar(100) null,
country varchar(100) null,
phone varchar(100) null
);
create table supplier (
id int primary key,
company_name varchar(100) not null,
contact_name varchar(100) null,
contact_title varchar(100) null,
city varchar(100) null,
country varchar(100) null,
phone varchar(100) null,
fax varchar(100) null
);
create table product (
id int primary key,
product_name varchar(100) not null,
unit_price decimal(12,2) null default 0,
package varchar(100) null,
is_discontinued boolean not null default false,
supplier_id int references supplier(id) not null
);
create table orders (
id int primary key,
order_date timestamp not null default now(),
order_number varchar(100) null,
total_amount decimal(12,2) null default 0,
customer_id int references customer(id) not null
);
create table order_item (
id int primary key,
unit_price decimal(12,2) not null default 0,
quantity int not null default 1,
order_id int references orders(id) not null,
product_id int references product(id) not null
);
If you only need product_name of products that were ordered, you can do the below sql. It involves only 2 tables.
select distinct p.product_name
from order_item o
join product p
on o.product_id = p.id
Postgres DB Fiddle

postgresql cannot insert data to newly added column

In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.