0

I have a table in which I have form id and the form fields column

=======================
form_id | form_fields
=======================
  1     | [abc,def]

Now there is one other table that contains the submitted form's response data like this

================================
device_id | form_id | response
================================
  101     |    1    | {abc:true}
================================
  102     |    1    | {def:true}
================================
  103     |    1    | {def:true}
================================
  101     |    2    | {xyz :true}

I want to get all the count of array values of form_fields of form_id 1 in the first table from the second table. I mean result be like

{abc:1,def:2}

One way is to make a simple json of all the responses then iterate one by one object of json and check the values. Is there any other efficient and better way to solve this? I am new to postgres so please ignore if this is too basic to ask.

Thanks!

1

1 Answer 1

1

I'm not really sure about this {abc:true} since i only work with values in columns, not key_value pairs, so please correct me if i'm wrong

but if they are also just values (like a varchar/...) this should work

select count(*) from your_table where form_id = 1 and response = 'abc'

select count(*) from your_table where form_id = 1 and response = 'def'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.