DECLARE @HOURSAGO int = 24
DECLARE @HoursAgoTime datetime = DATEADD(hour,-@HoursAgo, getutcdate())

select count(*)as clients
,uss.lastscanpackagelocation, uss.LastErrorCode
,        case
	when uss.LastErrorCode = '-2016409957' then 'Possible resource issues, connectivity or disk space (fix: retry scan)'
     	when uss.LastErrorCode = '-2016409966' then 'Group policy conflict, 2278557330, 0x87D00692'
	when uss.LastErrorCode = '-2016410107' then 'Scan tool has been removed, 2278557189, 0x87D00605'
	when uss.LastErrorCode = '-2016410110' then 'Content location request timeout occurred, 2278557186, 0x87D00602'
	when uss.LastErrorCode = '-2145075194' then 'Driver synchronization failed, 2149892102, 0x8024C006'
	when uss.LastErrorCode = '-2145107941' then 'WU_E_PT_HTTP_STATUS_PROXY_AUTH_REQ Same as HTTP status 407 - proxy authentication is required, 0x8024401B'
         when uss.LastErrorCode = '-2145107967' then 'Same as SOAPCLIENT_INITIALIZE_ERROR - initialization of the SOAP client failed, possibly because of an MSXML installation failure, 2149859329, 0x80244001'
	when uss.LastErrorCode = '-2147023781' then 'ERROR_SHUTDOWN_IN_PROGRESS, 0x8007045B'
	when uss.LastErrorCode = '-2147023843' then 'ERROR_SERVICE_REQUEST_TIMEOUT The service did not respond to the start or control request in a timely fashion, 0x8007041D'
	when uss.LastErrorCode = '-2147024888' then 'ERROR_NOT_ENOUGH_MEMORY The system is out of memory, 0x80070008'
        when uss.LastErrorCode = '-2145107961' then 'Same as SOAPCLIENT_SOAPFAULT - SOAP client failed because there was a SOAP fault for reasons of WU_E_PT_SOAP_* error codes, 2149859335, 0x80244007'
         when uss.LastErrorCode = '-2147467262' then 'No such interface supported (Fix: reinstall wuagent with windowsupdateagent30-x86.exe /wuforce /quiet /norestart)'
        when uss.LastErrorCode = '-2147467259' then 'Failed to create network share (fix: delete the likely corrupt file system32\\grouppolicy\\machine\\registry.pol)'
        when uss.LastErrorCode = '-2147417848' then 'RPC_E_Disconnected (guess is computer lost network connectivity)'
        when uss.LastErrorCode = '-2147221231' then '(did not find wsus reason, found exchange reason of MAPI_E_LOGON_FAILED'
        when uss.LastErrorCode = '-2147221164' then 'Class not registered (possible ATL.DLL corruption or needs reregister)'
        when uss.LastErrorCode = '-2147219966' then ''
        when uss.LastErrorCode = '-2147219963' then ''
        when uss.LastErrorCode = '-2147219822' then 'Possibly GPO Conflict (for Specify intranet Update Service Location)'
        when uss.LastErrorCode = '-2147219821' then 'fix: update WUA to latest version'
        when uss.LastErrorCode = '-2147217394' then 'WBEM_E_INVALID_NAMESPACE (wmi corrupt?)'
        when uss.LastErrorCode = '-2147024894' then 'BITS Service Failure' 
        when uss.LastErrorCode = '-2147024891' then 'Access Denied (fix: delete the likely corrupt file system32\\grouppolicy\\machine\\registry.pol)'        when uss.LastErrorCode = '-2147024882' then 'E OUTOFMEMORY'
        when uss.LastErrorCode = '-2147024873' then 'ERROR_CRC Data Error cyclic redundancy check (Likely BITS updated needed)'
        when uss.LastErrorCode = '-2147024864' then 'STIERR_SHARING_VIOLATION, The process cannot access the file because it is being used by another process (fix: rerun scan)'
        when uss.LastErrorCode = '-2147024809' then 'ERROR_INVALID_PARAMENTER - E_INVALIDARG,\"One or more arguments are not valid error - Invalid proxy server name was specified in the users IE settings '
        when uss.LastErrorCode = '-2147024770' then 'ERROR_MOD_NOT_FOUND,The specified module could not be found'
        when uss.LastErrorCode = '-2147023838' then 'ERROR_SERVICE_DISABLED,The service cannot be started (BITS or Automatic Updates service)'
        when uss.LastErrorCode = '-2147023436' then 'Error_Timeout'
        when uss.LastErrorCode = '-2147012894' then 'ERROR_INTERNET_TIMEOUT,The request has timed out (fix: re-run scan)'
        when uss.LastErrorCode = '-2147012881' then 'ERROR_INTERNET_LOGIN_FAILURE'
        when uss.LastErrorCode = '-2147012867' then 'ERROR_INTERNET_CANNOT_CONNECT,The attempt to connect to the server failed'
        when uss.LastErrorCode = '-2147012866' then 'ERROR_INTERNET_CONNECTION_ABORTED,The connection with the server has been terminated.'
        when uss.LastErrorCode = '-2146959355' then 'CO_E_Server_Exec_Failure (Automatic Updates Service does not start)'
        when uss.LastErrorCode = '-2145124338' then 'the SUS agent is attempting to parse an updates XML blob and has encountered data that is invalid'
        when uss.LastErrorCode = '-2145124326' then 'policy value is not set'
        when uss.LastErrorCode = '-2145124322' then 'call was aborted due to service stop or system shut down'
        when uss.LastErrorCode = '-2145124321' then 'no network connection is available to finish the operation' 
        when uss.LastErrorCode = '-2145124295' then ''
        when uss.LastErrorCode = '-2145120253' then 'MSI patching is disabled by policy'
        when uss.LastErrorCode = '-2145116137' then ''
        when uss.LastErrorCode = '-2145107960' then 'SOAPCLIENT_PARSEFAULT_ERROR failed in parsing SOAP fault'
        when uss.LastErrorCode = '-2145107958' then ' failed in parsing response (SOAPCLIENT)'
        when uss.LastErrorCode = '-2145107955' then 'SOAP_E_CLIENT  The message was malformed or incomplete. No reason to retry unless problem fixed.'
        when uss.LastErrorCode = '-2145107954' then 'The message was OK but server couldnt process at the moment. Same message *may* succeed at a later time.'
        when uss.LastErrorCode = '-2145107952' then 'The maximum allowed number of round trips to the server was exceeded (either re-run scan, or upgrade to latest WUA)'
        when uss.LastErrorCode = '-2145107947' then 'Server replied with InvalidCookie or ServerChanged Caller should refresh its internal state then repeat the call to Protocol Talker'
        when uss.LastErrorCode = '-2145107943' then 'Http status 404 - object not found'
        when uss.LastErrorCode = '-2145107934' then 'Http status 503 - temporarily overloaded (fix: manually invoke a Scan)'
        when uss.LastErrorCode = '-2145107924' then 'Winhttp SendRequest/ReceiveResponse failed with 0x2ee7 error. Either the proxy server or target server name can not be resolved. Corresponding to ERROR_WINHTTP_NAME_NOT_RESOLVED. Stop/Restart service or reboot the machine if you see this error frequently. '
        when uss.LastErrorCode = '-2145091577' then 'The caller asked for data that is not in the data store'
        when uss.LastErrorCode = '-2145091575' then 'The data store is in an invalid state because data that should be present is missing. This will occur if we try to fetch a linked row from another table and the linked row does not exist.  This can happen with EULAs, files, and/or localized properties referenced by an update.'
        when uss.LastErrorCode = '-2145091567' then 'Could not create an out of proc datastore object'
        when uss.LastErrorCode = '-2145091565' then 'the server has passed the same update to the client with two different revision ids'
		when uss.LastErrorCode = '-2145123272' then 'WU_E_PT_ENDPOINT_UNREACHABLE (possibly KB2734608 and/or new WUAgent needed)'
		when uss.LastErrorCode = '-2147023890' then 'FILE_INVALID (Potentially hash check failure)'
		when uss.LastErrorCode = '-2145123262' then 'WU_E_PT_OBJECT_FAULTED The Object is in a faulted state due to a previous error'
	    when uss.LastErrorCode = '-2147023836' then 'ERROR_SERVICE_DOES_NOT_EXIST Due to a bug in BITS 1.5 OOB setup, after BITS 1.5 install, the BITS service is deleted, but the 1.5 install returns success code. BITS 1.5 binaries are copied to the machine, but the service doesnt exist. This can also happen if the BITS service is explicitly deleted by running the command sc delete bits, 0x80070424'
        when uss.LastErrorCode = '-1073741819' then 'STATUS_ACCESS_VIOLATION (fix: delete the likely corrupt file system32\\grouppolicy\\machine\\registry.pol)'
when uss.LastErrorCode = '-939522994' then 'hrWriteConflict The write lock failed due to an outstanding write lock, 0xC800044E'
	when uss.LastErrorCode = '-939522288' then 'hrDiskFull, 0xC8000710' 
	when uss.LastErrorCode = '-939523074' then 'hrDiskIO, 0xC80003FE'
        when uss.LastErrorCode = '-939523586' then 'unable to write to the log file'
        when uss.LastErrorCode = '-939523567' then 'hrLogDiskFull (fix: Free up disk space) '
        when uss.LastErrorCode = '-939523085' then 'The computer is out of memory'
        when uss.LastErrorCode = '-939523079' then 'fix: BITS service stopped or needs updating'
        when uss.LastErrorCode = '-939523078' then 'hrReadVerifyFailure'
	when uss.LastErrorCode = '-939523082' then 'hrOutOfBuffers out of database page buffers, 0xC80003F6'
        when uss.LastErrorCode = '-939523064' then 'Unable to Access the File'
        when uss.LastErrorCode = '-939522493' then 'fix: stop automatic updates service. delete windir softwaredistribution folder. start service. Then got Error=0x8007000e (out of memory) possible fix is reboot computer.'
        when uss.LastErrorCode = '0' then 'Successful Scan'
		end as [Error Reason]
from v_updateScanStatus uss
  join v_statenames stat on stat.stateid = uss.lastscanstate
and stat.topictype ='501' and uss.lastscanpackagelocation like'http%'
and lastscantime > @HoursAgoTime and lastscantime < getutcdate()
group by uss.lastscanpackagelocation, uss.LastErrorCode
order by uss.lasterrorcode desc, uss.LastScanPackageLocation
