;with cte as (
select h.resourceid
,h.Category0 as 'UpdateCategory'
,h.Make0 as 'Make'
,h.ModelName0 as 'ModelName'
,h.ModelID0 as 'ModelID'
,h.Type0 as 'UpdateType'
,h.UpdateID0 as 'UpdateID'
,h.UpdateName0 as 'UpdateName'
,h.UpdateVersion0 as 'Update Version'
,h.URL0 as 'URL To download this update'
,h.UpdateDate0 as 'UpdateDate'
,Case when CHARINDEX('\',h.hwid0) >0 then  Left(h.hwid0, charindex('\',h.hwid0)-1)
else h.HWID0 end as 'DriverType'
,h.hwid0 as 'HWID'
from v_GS_HWUpdates0 h
where DATEDIFF(DAY,h.ScriptLastRan0,GETDATE()) < 21
)

 
Select
cte.UpdateCategory
,cte.Make
,cte.UpdateType
,cte.UpdateID
,cte.UpdateName
,cte.UpdateDate
,cte.[Update Version]
,cte.[URL To download this update]
,cte.DriverType
,cte.HWID
,Count(distinct cte.resourceid) as 'Count'
from cte
Where cte.make in ('HP','Dell')
and cte.UpdateType in ('Critical','Recommended','Routine','Urgent')

group by cte.UpdateCategory
,cte.Make
,cte.UpdateType
,cte.UpdateID
,cte.UpdateName
,cte.UpdateDate
,cte.[Update Version]
,cte.[URL To download this update]
,cte.DriverType
,cte.HWID
Order by 'count' DESC, cte.UpdateName