如果您帮助我们以下主题,我将非常有用:
select z1.nature_projet as typeCredit,COUNT(z1.nature_projet) as nbreTotalRejete, SUM(z1.montant_emprunt) as totalMontantRejete
FROM (
select * from dossier
where (EXTRACT(MONTH FROM date_affectation_banque ) = EXTRACT(MONTH FROM now())) and partner_id=1 and statut_dossier='Envoye'
) z1
where banque_statut_dossier='Rejete'
group by nature_projet**
以下图片描述了结果:
select z1.nature_projet as typeCredit,COUNT(z1.nature_projet) as nbreTotalAccorde, SUM(z1.montant_emprunt) as totalMontantAccorde
FROM (
select * from dossier
where (EXTRACT(MONTH FROM date_affectation_banque ) = EXTRACT(MONTH FROM now())) and partner_id=1 and statut_dossier='Envoye'
) z1
where banque_statut_dossier='Accorde'
group by nature_projet
以下图片描述了结果:
SELECT z1.nature_projet as typeCredit,
COUNT(z1.nature_projet) as nbreTotal, SUM(z1.montant_emprunt) as totalMontant
FROM (
select * from dossier
where (EXTRACT(MONTH FROM date_affectation_banque ) = EXTRACT(MONTH FROM now())) and partner_id=1 and statut_dossier='Envoye'
) z1
group by nature_projet
以下图片描述了结果:
我想将前两个查询的结果连接到一个,将最后一个查询连接起来,所以结果列如下:
TypeCredit,totalNbre,totalMontant,TotalNbreAccorde,TotalMontantAccorde,TottalNbreRejete,TotalMontantRejete
答案 0 :(得分:0)
您可以进行条件聚合。考虑:
select nature_projet,
count(*) filter(where banque_statut_dossier = 'Rejete' ) cnt_rejete,
count(*) filter(where banque_statut_dossier = 'Accorde') cnt_accorde,
count(*) filter(where banque_statut_dossier = 'Envoye' ) cnt_envoye,
sum(montant_emprunt) filter(where banque_statut_dossier = 'Rejete' ) montant_rejete,
sum(montant_emprunt) filter(where banque_statut_dossier = 'Accorde') montant_accorde,
sum(montant_emprunt) filter(where banque_statut_dossier = 'Envoye' ) montant_envoye
from dossier
where
partner_id = 1
and banque_statut_dossier in ('Rejete', 'Accorde', 'Envoye')
and date_affectation_banque >= date_trunc('month', current_date)
and date_affectation_banque < date_trunc('month', current_date) + interval '1 month'
group by nature_projet
请注意,我更改了日期过滤逻辑;您似乎打算在当月进行过滤,并且上述表达式使用针对半开间隔的直接过滤,比起依赖日期函数的原始策略更有效。