JeffersonLab / srm

System readiness manager app
https://ace.jlab.org/srm
MIT License
0 stars 0 forks source link

All Activity Report Query Fails #1

Closed slominskir closed 6 months ago

slominskir commented 6 months ago

The All Activity Report currently fails if there has been activity with:

ORA-01427: single-row subquery returns more than one row

The SQL that fails:

select *
from (select z.*, ROWNUM rnum
      from (with activity_subquery as (select count(group_signoff_history_id) as component_count,
                                              max(group_signoff_history_id)   as first_history_id,
                                              max(component_id)               as first_component_id,
                                              max(region_id)                  as first_region_id,
                                              trunc(modified_date, 'MI')      as modified_date,
                                              modified_username,
                                              system_id,
                                              group_id,
                                              comments,
                                              change_type,
                                              status_id
                                       from (select group_signoff_history_id,
                                                    component_id,
                                                    modified_date,
                                                    modified_username,
                                                    group_id,
                                                    comments,
                                                    change_type,
                                                    status_id,
                                                    component.system_id,
                                                    region_id,
                                                    name as component_name
                                             from group_signoff_history
                                                      inner join component using (component_id))
                                       group by trunc(modified_date, 'MI'), modified_username, system_id, group_id,
                                                comments, change_type, status_id),
                 combined as (select *
                              from (select modified_date,
                                           change_type,
                                           username as modified_username,
                                           record_id,
                                           component_id,
                                           inventory_activity.system_id,
                                           all_components.region_id,
                                           remark,
                                           null     as component_count,
                                           null     as group_id,
                                           null     as status_id,
                                           name     as component_name
                                    from inventory_activity
                                             left join all_components using (component_id))
                              union all
                              select modified_date,
                                     change_type,
                                     modified_username,
                                     first_history_id   as record_id,
                                     first_component_id as component_id,
                                     activity_subquery.system_id,
                                     first_region_id    as region_id,
                                     comments           as remark,
                                     component_count,
                                     group_id,
                                     status_id,
                                     name               as component_name
                              from activity_subquery
                                       left join all_components
                                                 on all_components.component_id = activity_subquery.first_component_id)
            select c.*, r.name as groupname, s.name as systemname, c2.unpowered_yn
            from combined c
                     left join responsible_group r on c.group_id = r.group_id
                     left join all_systems s on c.system_id = s.system_id
                     left join all_components c2 on c.component_id = c2.component_id
            order by modified_date desc, record_id desc) z
      where ROWNUM <= 100)
slominskir commented 6 months ago

The issue appears to be with the view srm_owner.inventory_activity, which contains:

https://github.com/JeffersonLab/srm/blob/c341b29e1d55d521bd88c112eb7cedff7764432f/container/oracle/initd.d/02_ddl.sql#L582-L636

Specifically this line:

https://github.com/JeffersonLab/srm/blob/c341b29e1d55d521bd88c112eb7cedff7764432f/container/oracle/initd.d/02_ddl.sql#L611

and this line:

https://github.com/JeffersonLab/srm/blob/c341b29e1d55d521bd88c112eb7cedff7764432f/container/oracle/initd.d/02_ddl.sql#L622