[postgresql-it] Conteggio record

  • From: "Pasquale Regina" <pasquale.regina@xxxxxxx>
  • To: <postgresql-it@xxxxxxxxxxxxx>
  • Date: Tue, 14 Jul 2020 21:17:36 +0200

Ciao a tutti.

Ancora qui a tentare di risolvere un mio problema.

Cerco di rappresentare tramite istogrammi una serie.

Di seguito il codice utilizzato

 

with drb_stats as (

select case when (dpr412 = 0 or dpr412 = 2) then '1_residential' 

                                               when dpr412 = 4 then
'2_offices'

                                               when dpr412 = 9 then
'3_commercial'

                                               when dpr412 = 14 then
'4_industrial'

                                               else
'5_other_not_residential' end as uso_edificio, 

                               count(a.co2) as co2_in_corso,

                               percentile_disc(0.005) within group (order by
co2) as min,

                               percentile_disc(0.995) within group (order by
co2) as max

     

from a_ape a 

                               left join a_ed_ui e on a.id_ed_ui =
e.id_ed_ui 

                               where date_part('year'::text,
a.vala)::integer >= 2020  and co2 > 0 and co2 is not null

                               group by uso_edificio

                               order by uso_edificio

),

 

histogram as (

   select uso_edificio, 

                                                width_bucket(co2, min, max,
198) as bucket,

          numrange(min(co2), max(co2), '[)') as intervallo, 

          count(a.co2) as frequenza 

                from drb_stats, 

                                               a_ape a 

                               left join a_ed_ui e on a.id_ed_ui =
e.id_ed_ui 

                               where date_part('year'::text,
a.vala)::integer >= 2020  and co2 > 0 and co2 is not null


               group by uso_edificio, bucket 

                order by uso_edificio asc, bucket 

)

 

select ROW_NUMBER () OVER() as progressivo, uso_edificio, bucket,
intervallo, 

                               frequenza, 

        repeat('*',

               (   frequenza::float

                / max(frequenza) over(partition by(uso_edificio))

                * 50

               )::int

        ) as bar

   from histogram;

 

 

drb_stats restituisce correttamente 

 


uso_edificio

co2_in_corso

min

max


1_residential

1318772

2,8

198,03


2_offices

55062

4,43

368,58


3_commercial

94658

2,79

552,89


4_industrial

57112

3,96

310,58


5_other_not_residential

25091

4,16

953,44

Con indicati il n di reord, il min e il max per il raggruppamento
uso_edificio

 

Con la select histogram suddivido l’intervallo compreso tra il min e il max
in bucket, li rappresento sottoforma di intervallo e conto gli elementi
presenti in ciascun intervallo.

Ciò per ciascuno dei 5 uso_edificio.

Il codice mi restituisce correttamente 1000 record (5x200) e riporta
correttamente i valori degli intervalli (es x il 4_industrial gli intervalli
hanno il primo bucket con min 3.96 e l’ultimo riporta il max 310.58). Il
problema è nel conteggio degli elementi in ciascun intervallo, la frequenza
che riporta il totale per cui se sommassi tutte le frequenze di
1_residential ho il totale 1550695 e così per la somma di 2_offices e via di
seguito, totale complessivo 7753475, il totale moltiplicato per 5.

Dove sbaglio?

Grazie per l’aiuto e l’attenzione che vorrete prestare.

Ciao, Pasquale.




==================================================

Questo messaggio e i suoi allegati sono indirizzati esclusivamente alle persone 
indicate e la casella di posta elettronica da cui e' stata inviata e' da 
qualificarsi quale strumento aziendale.
La diffusione, copia o qualsiasi altra azione derivante dalla conoscenza di 
queste informazioni sono rigorosamente vietate (art. 616 c.p, D.Lgs. n. 
196/2003 s.m.i. e GDPR Regolamento - UE 2016/679).
Qualora abbiate ricevuto questo documento per errore siete cortesemente pregati 
di darne immediata comunicazione al mittente e di provvedere alla sua 
distruzione. Grazie.

This e-mail and any attachments is confidential and may contain privileged 
information intended for the addressee(s) only.
Dissemination, copying, printing or use by anybody else is unauthorised (art. 
616 c.p, D.Lgs. n. 196/2003 and subsequent amendments and GDPR UE 2016/679).
If you are not the intended recipient, please delete this message and any 
attachments and advise the sender by return e-mail. Thanks.

==================================================

Other related posts: