Consulta SQL para mostrar una "columna total" en varias filas

assume I have the following table

claim_id | person_id | age_category | amount
--------------------------------------------
       1 |         1 |        adult |  10.00
       1 |         2 |        adult |  10.00
       1 |         3 |     juvenile |   8.00
       1 |         4 |        child |   5.00
       2 |         5 |        adult |  15.00
       3 |         6 |        adult |  12.00
       3 |         7 |        child |   6.00
     ...
     100 |       250 |        child |   7.00

So several people could belong to the same claim. What I already managed to achieve is such a result table:

category | total people |     amount
------------------------------------
adult    |          150 | 300'000.00
juvenile |           20 |  40'000.00
child    |           80 | 160'000.00

con la siguiente consulta:

select 
    age_category as "category"
    count(*) as "total people",
    sum(amount) as "amount"
from
    my_table
group by
    age_category

Is there a way how I can count the number of claims and display it in the same result table? E.g. something like:

category | total claims | total people |     amount
---------------------------------------|-----------
adult    |          100 |          150 | 300'000.00
juvenile |              |           20 |  40'000.00
child    |              |           80 | 160'000.00

¡Gracias por cualquier sugerencia!

P.S.: I am using DB2

preguntado el 28 de agosto de 12 a las 11:08

3 Respuestas

prueba esto:

select 
    age_category as "category",
    COUNT(distinct claim_id ) as "total_claims", --  <-- add this line 
    count(*) as "total people",
    sum(amount) as "amount"

from
    my_table
group by
    age_category

EDIT:

AS per your comment, use this query

select 
    age_category as "category",
    case when age_category='adult' then COUNT(distinct claim_id ) end 
                                                         as "total_claims" , 
    count(*) as "total people",
    sum(amount) as "amount"

from
    my_table
group by
    age_category  

Demostración de SQL Fiddle

Respondido 28 ago 12, 12:08

Thanks, I have thought about that already, but the result is not quite correct as it would output claim counts for 'juvenile' and 'child' as well. My problem is that these counts should not be listed as 'child'/'juvenile' are always part of an adult's claim. - user1613270

It's just a copy-paste of older answers. - hgulyan

@hgulyan: Are you serious..?? Your answer and my answer is same.. and I answered 11 min ago.. where as u 5 min ago... - jose g jose

Oh, didn't see, that you've updated your answer. Now it's right. - hgulyan

@hgulyan: If I have updated my Answer, It would have showed as "edited" - jose g jose

select 
    age_category as "category",
    sum(case when amount>0 then 1 else 0 end ) as "total claims",
    count(*) as "total people",
    sum(amount) as "amount"
from
    my_table
group by
    age_category

Respondido 28 ago 12, 11:08

Thanks, I have thought about that already, but the result is not quite correct as it would output claim counts for 'juvenile' and 'child' as well. My problem is that these counts should not be listed as 'child'/'juvenile' are always part of an adult's claim. - user1613270

Try to use count(DISTINCT ):

select 
    age_category as "category"
    count(*) as "total people",
    sum(amount) as "amount",
    count(distinct claim_id) as "total_claims"
from
    my_table
group by
    age_category

or also try to use this:

select t.*,
(select count(*) from

(select distinct claim_id from my_table 
   where my_table.age_category=t.category) as d
 ) as "total_claims"
from
(
select 
    age_category as "category",
    count(*) as "total people",
    sum(amount) as "amount"
from
    my_table
group by
    age_category
  ) as t

Respondido 28 ago 12, 12:08

this doesn't count the claims but sums up the claim id's - user1613270

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.