const
  cGUID = 'CFD714556B73460ABDAC8F3B582BCAFC';

  procedure OpenNewTabForGroup(AGroup: TCatalogPropGroup);
  var
    AElem: TidElement;
  begin
    // delete results from a former run
    PublicCatalog.RemoveItemsFromTempList(AGroup.IncludeItems);

    // do search
    AElem := TidElement.Create(nil);
    try
      AElem.Prop['OpenNewTab'] := True;
      AElem.Data := AGroup;
      PublicBroadcast(nil, 'ShowGroupResults', AElem);
    finally
      AElem.Free;
    end;
  end;

  function MainFilter: WideString;
  begin
    result := 'select GUID ' +
              'from   idCatalogItem i ' +
              'where  i.idFileType = ''HEIC'' ' +
              'and    (' + 
              '       exists (' + 
              '                select  1 ' + 
              '                from    idcatalogItem i2 ' +
              '                where   i2.PathGUID = i.PathGUID ' +
              '                and     upper(i2.FileName) = upper(replace(i.FileName, ''.HEIC'', ''.MOV'')) ' + ' ' +
              '              )  ' +
              '       or exists (' + 
              '                   select  1 ' + 
              '                   from    idImageVersion i2 ' +
              '                   where   i2.PathGUID = i.PathGUID ' +
              '                   and     upper(i2.FileName) = upper(replace(i.FileName, ''.HEIC'', ''.MOV'')) ' + ' ' +
              '                 )  ' +
              '       ) ' +
              'union all ' + 
              'select GUID ' +
              'from   idCatalogItem i ' +
              'where  i.idFileType = ''MOV'' ' +
              'and    ( ' +
              '       exists (' + 
              '                select  1 ' + 
              '                from    idcatalogItem i2 ' +
              '                where   i2.PathGUID = i.PathGUID ' +
              '                and     upper(i2.FileName) = upper(replace(i.FileName, ''.MOV'', ''.HEIC'')) ' + ' ' +
              '              )  ' +
              '       or exists (' + 
              '                   select  1 ' + 
              '                   from    idImageVersion i2 ' +
              '                   where   i2.PathGUID = i.PathGUID ' +
              '                   and     upper(i2.FileName) = upper(replace(i.FileName, ''.MOV'', ''.HEIC'')) ' + ' ' +
              '                 )  ' +
              '       ) ' +
              'union all ' +
              'select GUID ' +
              'from   idImageVersion i ' +
              'where  i.idFileType = ''HEIC'' ' +
              'and    ( ' +
              '       exists (' + 
              '                select  1 ' + 
              '                from    idcatalogItem i2 ' +
              '                where   i2.PathGUID = i.PathGUID ' +
              '                and     upper(i2.FileName) = upper(replace(i.FileName, ''.HEIC'', ''.MOV'')) ' + ' ' +
              '              )  ' +
              '       or exists (' + 
              '                   select  1 ' + 
              '                   from    idImageVersion i2 ' +
              '                   where   i2.PathGUID = i.PathGUID ' +
              '                   and     upper(i2.FileName) = upper(replace(i.FileName, ''.HEIC'', ''.MOV'')) ' + ' ' +
              '                 )  ' +
              '       ) ' +
              'union all ' + 
              'select GUID ' +
              'from   idImageVersion i ' +
              'where  i.idFileType = ''MOV'' ' +
              'and    ( ' +
              '       exists (' + 
              '                select  1 ' + 
              '                from    idcatalogItem i2 ' +
              '                where   i2.PathGUID = i.PathGUID ' +
              '                and     upper(i2.FileName) = upper(replace(i.FileName, ''.MOV'', ''.HEIC'')) ' + ' ' +
              '              )  ' +
              '       or exists (' + 
              '                   select  1 ' + 
              '                   from    idImageVersion i2 ' +
              '                   where   i2.PathGUID = i.PathGUID ' +
              '                   and     upper(i2.FileName) = upper(replace(i.FileName, ''.MOV'', ''.HEIC'')) ' + ' ' +
              '                 )  ' +
              '       ) ' +
              '';
  end;

  procedure RunSearch;
  var
    ADs: TDBXOMClientDataSet;
    AGroup: TCatalogPropGroup;
  begin
    ADs := PublicCatalog.NewDataset;
    ADs.CommandText := 'select ' + PublicCatalog.FullColumnList('i.') + ' ' +
                       'from   v_catalogitem i ' +
                       'inner join (' + MainFilter + ') as f on f.GUID = i.GUID ' +
                       'UNION ALL ' +
                       'select ' + PublicCatalog.FullColumnList('i.') + ' ' +
                       'from   v_catalogitemversion i ' +
                       'inner join (' + MainFilter + ') as f on f.GUID = i.GUID ' +
                       'order by FileName ' +
                       '';
    ADs.LockType := ReadLockType;
    ADs.OpenSet;

    AGroup := TCatalogPropGroup.Create(nil);
    AGroup.GroupName := 'HEIC with MOV Pairs';
    AGroup.IncludeItems.GUID := cGUID;  // fix the GUID
    AGroup.IncludeItems.AddDataset(ADs, True);
    AGroup.IncludeItems.Disconnect;

    OpenNewTabForGroup(AGroup);

    PublicCatalog.FreeDataset(ADs);

    AGroup.Free;
  end;

begin
  RunSearch;
end;

