sql - add parameters to select case db2 -


i have following select statement:

 select (case when age_years >= 18 , age_years < 30 '18-29'                   when age_years < 50 '30-49'                                             when age_years < 70 '50-69'                                              when age_years < 100 '70-100'                                          end) age_range, count(*) num                                       info                                                  group (case when age_years >= 18 , age_years < 30 '18-29'             when age_years < 50 '30-49'                                             when age_years < 70 '50-69'                                             when age_years < 100 '70-100'                                           end)                                                                         order min(age_years);   

output

 age_range          num    ---------+---------+----- 18-29              828    30-49             2510    50-69             2014    70-100             649    

now want add column percentage of male/female parameter "gender" (0 or 1) in "info" , parameter of table "paytb", sum of transactions "acaureq_aureq_tx_dt_ttlamt". both tables share cont_id. should this:

 age_range          num      gender       transaction amount average ---------+---------+--------------------------------------------------- 18-29              828        50%         2000 $ 30-49             2510        ??          ???  50-69             2014        ??          ??? 70-100             649   

i think should you. adding more aggregates. changed counts, because assumed there 1 many relationship between new table , client table.

 select (case when age_years >= 18 , age_years < 30 '18-29'                   when age_years < 50 '30-49'                                             when age_years < 70 '50-69'                                              when age_years < 100 '70-100'                                          end) age_range,               count(distinct <pk_client_info>) num ,              sum(case when client_info = 'male' 1 else 0 end) / count(distinct <pk_client_info>) 'male/female'              ,sum(acaureq_aureq_tx_dt_ttlamt) / count(*) 'total-amount avg'          info c          left paytb t          on c.cont_id = t.cont_id                                                  group (case when age_years >= 18 , age_years < 30 '18-29'             when age_years < 50 '30-49'                                             when age_years < 70 '50-69'                                             when age_years < 100 '70-100'                                           end)                                                                         order min(age_years);  

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -