SELECT
  (
    (
      ((piv.nursery_id || '_' :: text) || piv.specie_id) || '_' :: text
    ) || piv.type
  ) AS id,
  piv.nursery_id,
  piv.city_id,
  piv.specie_id,
  COALESCE(
    sum(
      CASE
        WHEN (
          piv.type = 'INCREASE' :: "InventoryTransactionType"
        ) THEN piv.quantity
        ELSE NULL :: bigint
      END
    ),
    (0) :: numeric
  ) AS increase,
  COALESCE(
    sum(
      CASE
        WHEN (
          piv.type = 'DECREASE' :: "InventoryTransactionType"
        ) THEN (- piv.quantity)
        ELSE NULL :: bigint
      END
    ),
    (0) :: numeric
  ) AS decrease,
  sum(
    CASE
      WHEN (
        piv.type = 'INCREASE' :: "InventoryTransactionType"
      ) THEN piv.quantity
      ELSE (- piv.quantity)
    END
  ) AS total
FROM
  plant_inventory_view piv
GROUP BY
  piv.nursery_id,
  piv.specie_id,
  piv.city_id,
  piv.type
ORDER BY
  piv.nursery_id;