SELECT
   r5actchecklists.*,
   (
      CASE
         WHEN
            (
               SELECT
                  uco_rcode
               FROM
                  r5ucodes
               WHERE
                  uco_code        = ack_group_label
                  AND uco_rentity = 'GRPL'
            )
            = 'GBEQ'
            THEN (ack_group_label ||
            '#' ||
               ack_object ||
            '#' ||
               ack_object_org)
            ELSE ack_group_label
      END
   )
   AS ack_group_key,
   (
      SELECT
         CAST(COUNT(*) AS INT)
      FROM
         r5documents
      WHERE
         doc_entitycode=ack_code and doc_rentity='WACL'
   )
                                                AS ack_doccount        ,
   ( SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'SEVE' AND UCO_CODE = ack_severity) AS ack_severity_display,
   ( SELECT NCT_DESC FROM r5nonconformitytypes WHERE NCT_CODE = ACK_NONCONFORMITYTYPE) AS ack_nonconformitytype_display,
   ob.obj_linearrefuom                          AS linearrefuom        ,
   ob.obj_desc                                  AS equipmentdescription,
   ob.obj_desc                                  AS ack_object_desc     ,
   ob.obj_class                                 AS ack_object_class    ,
   ob.obj_class_org                             AS ack_object_classorg ,
   ob.obj_gisobjid                              AS ack_object_gisobjid ,
   asp_desc                                     AS ack_aspect_desc     ,
   poi_desc                                     AS ack_point_desc      ,
   ack_frompoint + COALESCE(ob.obj_frompoint,0) AS frompoint           ,
   ack_topoint   + COALESCE(ob.obj_frompoint,0) AS topoint             ,
   fnd_desc                                     AS ack_finding_desc    ,
   (
      CASE
         WHEN
            (
               SELECT
                  COUNT(*)
               FROM
                  r5addetails
               WHERE
                  add_code       = ack_taskchecklistcode
                  AND add_rentity='TCLI'
                AND add_lang=<parameter.language>
            )
            > 0
            THEN '+'
            ELSE '-'
      END
   )
   AS ack_comments,
    (SELECT
    ADD_TEXT
    FROM
    r5addetails
    WHERE
    add_code = ack_taskchecklistcode
    AND add_rentity='TCLI'  AND add_lang=<parameter.language>) AS ack_comment_text
FROM r5actchecklists
   JOIN
      r5activities act
      ON
         ack_act       = act_act
         AND ack_event = act_event
   LEFT OUTER JOIN
      r5findings fin
      ON
         fnd_code= ack_finding
   LEFT OUTER JOIN
      r5aspects asp
      ON
         asp_code= ack_aspect
   LEFT OUTER JOIN
      r5points poi
      ON
         poi_point         = ack_point
         AND poi_pointtype =ack_pointtype
         AND poi_object    = ack_object
         AND poi_object_org= ack_object_org
   LEFT OUTER JOIN
      r5objects ob
      ON
         ack_object         = obj_code
         and ack_object_org = obj_org
   LEFT OUTER JOIN
      r5taskchecklists tl
      ON
         act.act_task   = tl.tch_task
         and tl.tch_code= ack_taskchecklistcode
WHERE ack_event   =<param.workordernum> AND ack_act = <param.activitytrade>
ORDER BY CASE ack_group_key
      WHEN NULL
         THEN 'zzzzzzz'
         ELSE ack_group_key
   END              ,
   ack_routesequence, ack_linearlocation, ack_object, ack_object_org, ack_point, ack_sequence,ack_occurrence
