How to get the first 4 rows of each record in table and append to the select instead rather than rows?

Viewed 27

I have two tables:

  1. documents table
  2. audits table.

audits table is a polymorphic table and has auditable_type and auditable_id which is linked to the id of documents table. audits table also has event and new_values column.

Here's the table schema.

enter image description here

documents table has user_defined_field JSON column.

enter image description here

enter image description here

My goal is I want to get the first 4 values of the status field in user_defined_field JSON column. There will be a column in the report of the web app to display the 1st Status, 2nd Status, 3rd Status, 4th Status.

So basically, it's like the documents table is join to audits table and should get the first 4 record of the document id and display it in the SQL select as status_1, status_2, status_3, and status_4

Thank you.

0 Answers
Related