2
The purpose of the query is to list, within a user-defined period, all states and their total number of independent quotes if zero. Customers must be active (status 30) and have city id number mentioned.
The table vangebot
has the quotations, with id of the customer’s address and date of issue:
angebotid | eingabeDatum | adressebestellerid - nº do endereço
201970110 | 01/06/2019 | 26087
201970111 | 25/05/2018 | 15305
201970112 | 23/06/2016 | 18447
The table gpartner
has information about the customer:
partnerid | partnername | status
24270 | Oliveira | 30
24669 | Medsíntese | 30
24665 | Assessoria | 30
21122 | X | 30
The table gadresse
has the address linked to the client in the table gpartner
, with the city number:
adresseid | partnerid | adresseName | postfachplz - nº da cidade
26087 | 24270 | R. Alviverde | 193
15305 | 24669 | R. 358 | 2043
18447 | 24665 | Av. Leopoldo | 1890
14151 | 21122 | R. Barroca | 30
The table its_microRegion
has the city and the micro-region linked according to the address mentioned in the table gadresse
:
id | city | uf - nº da uf
193 | Porto Alegre | 23
2043 | Belo Horizonte | 17
1890 | Rio de Janeiro | 19
30 | Alecrim | 12
The table its_Region
has the name of the state, linked to the table its_microRegion
:
id | uf | regionName
23 | RS | Rio Grande do Sul
17 | MG | Minas Gerais
19 | RJ | Rio de Janeiro
12 | GO | Goiás
I have drawn up the following consultation:
select its_Region.uf, its_Region.regionName, count(vangebot.angebotid) as qtdCotacoes from vangebot
left join gpartner on vangebot.kundeid = gpartner.partnerid
left join gadresse on gadresse.adresseid = vangebot.adressebestellerid
left join its_microRegion on gadresse.postfachplz = its_microRegion.id
inner join its_Region on its_microRegion.uf = its_Region.id
where vangebot.eingabeDatum between '2016-01-01' and '2019-06-02'
and gpartner.status = 30 and gadresse.postfachPlz <> ''
group by its_Region.uf order by qtdCotacoes desc;
Upshot:
uf | regionName | qtdCotacoes
RS | Rio Grande do Sul | 1
MG | Minas Gerais | 1
RJ | Rio de Janeiro | 1
What should I show:
uf | regionName | qtdCotacoes
RS | Rio Grande do Sul | 1
MG | Minas Gerais | 1
RJ | Rio de Janeiro | 1
GO | Goiás | 0
However, the result presented only mentions the states that have linked quotations and the others do not. What should I do?
I didn’t create the tables to actually test, but you probably have to use left_join or right_join instead of inner_join. From a researcher on this
– Éverson da Luz
Can make a DB Fiddle to facilitate finding the problem?
– Sorack
@Sorack, I just inserted the fiddle. Can you take a look?
– Gabriel Maltha Luiz