0

I have a table, called feedback, that contains: no, date, id, message, value.

  • no The row number.
  • id The employee id.
  • message Feedback from a customer.
  • value The feedback type. -1, 0, or 1.

Now I have a view which provides me with id, messages, total, value.

  • id The employee id.
  • messages An array of message which share id and value.
  • total The total number of rows which share id and value.
CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `vw_feedback` AS
    SELECT 
        `feedback`.`no` AS `no`,
        `feedback`.`id` AS `id`,
        JSON_ARRAYAGG(`feedback`.`message`) AS `messages`,
        `feedback`.`value` AS `value`,
        COUNT(0) AS `total`
    FROM
        `feedback`
    GROUP BY `feedback`.`id` , `feedback`.`value`
    ORDER BY `feedback`.`date` DESC

What I would like to do, is instead have id, negative_messages, positive_messages, total_negative, total_positive, total.

  • id The employee id.
  • negative_messages An array of the last 5 message for that id which have a value < 0.
  • positive_messages An array of the last 5 message for that id which have a value > 0.
  • total_negative Total number of rows for that id with value < 0.
  • etc...

I could use help on where to begin.

Stev
  • 129
  • 7
  • I don't know how well it applies to `JSON_ARRAYAGG`, but see https://stackoverflow.com/questions/2129693/mysql-using-limit-within-group-by-to-get-n-results-per-group for how to get 5 results per group. – Barmar Dec 17 '19 at 22:41
  • Doesn't this view give you `GROUP BY` errors given that you're running MySQL 8? – Nick Dec 17 '19 at 23:17
  • @Nick- It does not. Should it? I'm not very good with MySQL. Even with Barmar's response I'm scratching my head. – Stev Dec 17 '19 at 23:19

0 Answers0