Search code examples
sqlimpala

Calculate percentage between two tables with group by -SQL (Impala)


I'm working on Impala (Cloudera) and I have two tables, Customers and Arrangements. Customer table has the following columns:

customercrs | customertype| 
------------+-------------+
 1000       | NP          |  
 100000     | NP          |   
 100001     | NP          |  
 100002     | GROUP       |  
 100023     | GROUP       |
 100024     | INDIRECT    |

Arrangement table:

customercrs | arrangementid| 
------------+--------------+
 1000       | 11000000361  |  
 100000     | 11000000370  |  
 100000     | 11000000434  |
 100000     | 11000000426  |
 100001     | 11000000418  | 
 100001     | 11000000400  |
 100001     | 11000000396  |
 100001     | 11000000388  |
 100002     | 11000000591  |  
 100002     | 11000000582  |
 100023     | 11000000574  |
 100024     | 11000000566  |
 100024     | 11000000558  |

I want to calculate the percentage of arrangements per customertype. Something like:

customertype | percentage  |
-------------+-------------+
 NP          | 62%         |
 GROUP       | 23%         |
 INDIRECT    | 15%         |

I tried the following sql query, but it didn't work. Any idea?

select customertype, count(*)/(select count(*) from arrangements)
from customers as a, arrangements_sample as b
where a.customercrs = b.customercrs
group by a.customertype

Thanks!!!


Solution

  • Try to join the subselect, I used max as group function, but min or avg would work too...

    select customertype, count(*)/max(c.total)
    from customers as a, arrangements_sample as b, (select count(*) as total from 
    arrangements) as c
    where a.customercrs = b.customercrs
    group by a.customertype