My table is like this:

```
create table alphabet_soup(
id numeric,
index json bigint
);

```

my data looks like this:

```
(id, json) looks like this: (1, '{('key':1,'value':"A"),('key':2,'value':"C"),('key':3,'value':"C")...(600,"B")}')

```

How do I sum across the json for number of A and number of B and do % of the occurence of A or B? I have about 6 different types of values (ABCDEF), but for simplicity I am just looking for a comparison of 3 values.

I am trying to find something to help me calculate the % of occurrence of a value from a key value pair in json. I am using postgres 9.4. I am new to both json and postgres, and I am landing on the same json functions manual page of postgres over and over.

I have managed to find a sum, but how to calculate the % in a nested select and display the key and values in increasing order of occurence like follows:

```
value    | occurence   |  %
====================================
A        |      300    |  50      
B        |      198    |  33    
C        |      102    |  17

```

The script I am using for the sum is :

```
 select id, index->'key'::key as key
    sum(case when (1,index::json->'1')::text = (1,index::json->'2')::text 
 then 1
    else 0
end)/count(id) as res
from
alphabet_soup
group by id;
limit 10;

```

I get an output as follows:

```
 column "alphabet_soup.id" must appear in the group by clause or be used in an aggregate function.

```

Thanks for the comment Patrick. Sorry I forgot to add I am using postgres 9.4