select resources.resourcename as "Asset Name", systeminfo.model, productType.componentTypename as "Product Type", longtodate(resourcestatehistory.starttime) "State changed at" , prevstate.statedesc as "prev state", currentstate.statedesc as "current state",resuser.first_name "User", statech.first_name as "state changed by", resourcestatehistory.comments "comments for changing state", resourceownerhistory.comments from resourcestatehistory left join resourceownerhistory on resourcestatehistory.STATEHISTORYID = ResourceOwnerHistory.STATEHISTORYID left join resources on resources.resourceid = resourcestatehistory.resourceid LEFT JOIN resourcestate fortesting on fortesting.resourcestateid = resources.resourcestateid left join aaauser resuser on resuser.user_id = resourceownerhistory.userid left join aaauser taskdone on taskdone.user_id = resourceownerhistory.taskuserid left join aaauser resOwner on resOwner.user_id = resourceownerhistory.resourceownerid left join systeminfo on systeminfo.workstationid = resources.resourceid LEFT JOIN departmentDefinition on departmentDefinition.deptid = resourceownerhistory.deptid left join resourcestate prevstate on prevstate.resourcestateid = resourcestatehistory.prevresourcestateid left join resourcestate currentstate on currentstate.resourcestateid = resourcestatehistory.resourcestateid left join aaauser statech on statech.user_id = resourcestatehistory.userid LEFT JOIN ComponentDefinition product ON resources.COMPONENTID = product.COMPONENTID LEFT JOIN ComponentType productType ON product.COMPONENTTYPEID = productType.COMPONENTTYPEID where productType.COMPONENTTYPENAME != 'Software' and systeminfo.workstationid is not null and resources.resourceid not in (select resources.resourceid from resources left join systeminfo on systeminfo.workstationid = resources.resourceid left join (select resourcestatehistory.statehistoryid, aaa.statedesc "current state", bbb.statedesc as "previous state", resourcestatehistory.prevresourcestateid, resourcestatehistory.resourceid, (select rsh.statehistoryid from resourcestatehistory rsh where rsh.endtime = resourcestatehistory.starttime and resourcestatehistory.resourceid = rsh.resourceid) "check" from resourcestatehistory left join resourcestate aaa on aaa.resourcestateid = resourcestatehistory.resourcestateid left join resourcestate bbb on bbb.resourcestateid = resourcestatehistory.prevresourcestateid where resourcestatehistory.resourcestateid in (4,5) and resourcestatehistory.endtime is null ) "statehistory" on "statehistory".resourceid = resources.resourceid left join resourcestatehistory rss on rss.statehistoryid = "statehistory".statehistoryid left join resourceownerhistory roh on roh.statehistoryid = "statehistory"."check" left join aaauser on aaauser.user_id = roh.userid left join departmentdefinition dd on dd.deptid = roh.deptid where systeminfo.workstationid is not null and "statehistory".prevresourcestateid in (1,2,3,4,5)) and fortesting.resourcestateid =2 and resourcestatehistory.starttime>= datetolong('2022-08-31 00:00:00') and resourcestatehistory.starttime<=datetolong('2022-09-6 23:59:59') order by 1, resourcestatehistory.starttime desc, 10 asc