select t1.siteid
     , t1.ownerid
     , t1.ownertable
     , t1.ftype
     , t1.createby
     , t1.createdate
     , t1.postfix
     , t1.serialnumber
     , REPLACE(t1.fdocument, '.' + t1.postfix, '') as fdocument
     , t1.tattachmentid
     , t1.fobsurl                                  as url
     , case
           when t1.postfix in ('jpg','jpeg', 'png', 'mp4', 'mov') then fobsurl_minimage
           else t1.fobsurl end                     as show_url
     , fobsurl_compressimage as compressimage_url
     , t1.fobsurl_hls
     , count(t2.tattachmentid)                     as fdownloadcount
     , t1.contentlength
from tattachment t1
         left join tattachment_downlog t2 on t1.siteid = t2.siteid and t1.tattachmentid = t2.tattachmentid
where t1.siteid =$siteid$ and t1.ownertable=$ownertable$ and t1.ownerid=$ownerid$ and t1.ftype=$ftype$
group by t1.siteid
        , t1.ownerid
        , t1.ownertable
        , t1.ftype
        , t1.createby
        , t1.createdate
        , t1.postfix
        , t1.serialnumber
        , REPLACE(t1.fdocument, '.' + t1.postfix, '')
        , t1.tattachmentid
        , t1.fobsurl
        , t1.postfix, case
    when t1.postfix in ('jpg','jpeg', 'png', 'mp4', 'mov') then fobsurl_minimage
else
t1.fobsurl end, fobsurl_compressimage,t1.fobsurl_hls, t1.contentlength
order by case when t1.ftype='cover' then 0 else 1 end,t1.ftype,t1.createdate